定義通用的可經過lambda表達式樹來獲取屬性信息

咱們通常獲取某個類型或對象的屬性信息均採用如下幾種方法:html

1、經過類型來獲取屬性信息
var p= typeof(People).GetProperty("Age");//獲取指定屬性
var ps = typeof(People).GetProperties();//獲取類型的全部屬性

2、經過實例來獲取屬性信息
People people = new People();
var pro = people.GetType().GetProperty("Age");
var pros = people.GetType().GetProperties();

這兩種方法都有其弊端,第一種與第二種方法,在獲取單個屬性信息時,都須要硬編碼寫入常量屬性名稱,這樣在編譯時並不會報錯,只有在運行時才知道異常,第二種方法若只是須要獲取屬性的類型名稱等基本信息,不須要屬性的值,就不須要實例化類型。編碼

鑑於以上緣由,我定義了通用的可經過lambda表達式樹來獲取屬性信息,使用方便,能解決上述問題,且有智能提示,若出現錯誤,編譯時就能報出,方法定義代碼以下:htm

        /// <summary>
        /// 獲取指定屬性信息(非String類型存在裝箱與拆箱)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="select"></param>
        /// <returns></returns>
        public static PropertyInfo GetPropertyInfo<T>(Expression<Func<T,dynamic>> select) 
        {
            var body = select.Body;
            if (body.NodeType == ExpressionType.Convert)
            {
                var o = (body as UnaryExpression).Operand;
                return (o as MemberExpression).Member as PropertyInfo;
            }
            else if (body.NodeType == ExpressionType.MemberAccess)
            {
                return (body as MemberExpression).Member as PropertyInfo;
            }
            return null;
        }

        /// <summary>
        /// 獲取指定屬性信息(須要明確指定屬性類型,但不存在裝箱與拆箱)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TR"></typeparam>
        /// <param name="select"></param>
        /// <returns></returns>
        public static PropertyInfo GetPropertyInfo<T, TR>(Expression<Func<T, TR>> select)
        {
            var body = select.Body;
            if (body.NodeType == ExpressionType.Convert)
            {
                var o = (body as UnaryExpression).Operand;
                return (o as MemberExpression).Member as PropertyInfo;
            }
            else if (body.NodeType == ExpressionType.MemberAccess)
            {
                return (body as MemberExpression).Member as PropertyInfo;
            }
            return null;
        }

        /// <summary>
        /// 獲取類型的全部屬性信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="select"></param>
        /// <returns></returns>
        public static PropertyInfo[] GetPropertyInfos<T>(Expression<Func<T,dynamic>> select)
        {
            var body = select.Body;
            if (body.NodeType == ExpressionType.Parameter)
            {
               return (body as ParameterExpression).Type.GetProperties();
            }
            else if(body.NodeType==ExpressionType.New)
            {
                return (body as NewExpression).Members.Select(m => m as PropertyInfo).ToArray();
            }
            return null;
        }

使用很簡單:對象

// People類型定義
class People
    {
        public string Name
        { get; set; }

        public int Age
        { get; set; }

        public string Sex
        { get; set; }

        public bool IsBuyCar
        { get; set; }

        public DateTime? Birthday
        { get; set; }
    }


//如下是使用方法:

var p = GetPropertyInfo<People>(t => t.Age);//獲取指定屬性
var ps1 = GetPropertyInfos<People>(t => t);//獲取類型全部屬性
var ps2 = GetPropertyInfos<People>(t => new { t.Name, t.Age });//獲取部份屬性

  

注意dynamic類型也存在裝箱與拆箱的問題,詳見這篇博文:http://www.cnblogs.com/yank/p/4177619.htmlblog

相關文章
相關標籤/搜索