• 定义通用的可通过lambda表达式树来获取属性信息


    我们一般获取某个类型或对象的属性信息均采用以下几种方法:

    1
    2
    3
    4
    5
    6
    7
    8
    一、通过类型来获取属性信息
    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表达式树来获取属性信息,使用方便,能解决上述问题,且有智能提示,若出现错误,编译时就能报出,方法定义代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    /// <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;
    }

    使用很简单:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    // People类型定义
    class People
        {
            public string Name
            getset; }
     
            public int Age
            getset; }
     
            public string Sex
            getset; }
     
            public bool IsBuyCar
            getset; }
     
            public DateTime? Birthday
            getset; }
        }
     
     
    //以下是使用方法:
     
    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

     
     
  • 相关阅读:
    taotao-manager-web/pom.xml
    sublime使用说明
    taotao-manager-web(表现层),初始生成pom报错解决
    通用表空间
    InnoDB存储引擎结构介绍
    关于双主
    半同步复制
    基于gtid的复制
    多源复制和级联复制
    MySQL复制进阶
  • 原文地址:https://www.cnblogs.com/sjqq/p/8421344.html
Copyright © 2020-2023  润新知