标签:
我们一般获取某个类型或对象的属性信息均采用以下几种方法:
一、通过类型来获取属性信息 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
标签:
原文地址:http://www.cnblogs.com/zuowj/p/4452046.html