我們一般獲取某個類型或對象的屬性信息均采用以下幾種方法:
一、通過類型來獲取屬性信息 var p= typeof(People).GetProperty("Age");//獲取指定屬性 var ps = typeof(People).GetProperties();//獲取類型的所有屬性 二、通過實例來獲取屬性信息 People people = new People(); var pro = people.GetType().GetProperty("Age"); var pros = people.GetType().GetProperties();
這兩種方法都有其弊端,第一種與第二種方法,在獲取單個屬性信息時,都需要硬編碼寫入常量屬性名稱,這樣在編譯時並不會報錯,只有在運行時才知道異常,第二種方法若只是需要獲取屬性的類型名稱等基本信息,不需要屬性的值,就不需要實例化類型。
鑒於以上原因,我定義了通用的可通過lambda表達式樹來獲取屬性信息,使用方便,能解決上述問題,且有智能提示,若出現錯誤,編譯時就能報出,方法定義代碼如下:
/// <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.html