Defines generic attributes that can be obtained through the lambda Expression Tree. lambda expressions
Generally, the following methods are used to obtain the property information of a type or object:
1. Obtain the property information through the type var p = typeof (People ). getProperty ("Age"); // obtain the specified property var ps = typeof (People ). getProperties (); // get all properties of the type. 2. Get the property information People people = new People () through the instance. var pro = people. getType (). getProperty ("Age"); var pros = people. getType (). getProperties ();
Both methods have their drawbacks. The first and second methods require hard encoding to write the constant attribute name when obtaining a single attribute information, so that no error is reported during compilation, the exception is only known during running. If you only need to obtain basic information such as the type name of the attribute and do not need the attribute value, you do not need to instantiate the type.
In view of the above reasons, I have defined a general method for obtaining attribute information through the lambda Expression Tree, which is convenient to use and can solve the above problems. In addition, there are smart prompts. If an error occurs, the method definition code is as follows:
/// <Summary> /// obtain the specified attribute information (non-String-type packing and unpacking exist) /// </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> // obtain the specified attribute information (the attribute type must be specified explicitly, but no packing or unpacking exists) /// </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> /// obtain all attribute information of the type /// </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 ;}
Easy to use:
// Class People {public string Name {get; set;} public int Age {get; set;} public string Sex {get; set;} defined by the People type ;} public bool IsBuyCar {get; set;} public DateTime? Birthday {get; set ;}/// use var p = GetPropertyInfo <People> (t => t. age); // obtain the specified property var ps1 = GetPropertyInfos <People> (t => t ); // obtain all types of attributes var ps2 = GetPropertyInfos <People> (t => new {t. name, t. age}); // get some attributes
Note dynamic type also has the problem of packing and unpacking, see this blog: http://www.cnblogs.com/yank/p/4177619.html