• .NET 表达式目录树


    Expression我们称为是表达式树,是一种数据结构体,用于存储需要计算,运算的一种结构,这种结构可以只是存储,而不进行运算。通常表达式目录树是配合Lambda一起来使用的,lambda可以是匿名方法,当然也可以使用Expression来动态的创建!在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    我们可以通过下面创建表达式目录树,我们称之为A种方式:

    Expression> lambda = x => x.Id.ToString().IndexOf(“5”) >= 0;
    我们还可以使用Expression来动态创建,我们称之为B种方式:
    ///
    /// 实体类
    ///
    public class People
    {
    public int Age { get; set; }
    public string Name { get; set; }
    public int Id;
    }
    var peopleParam = Expression.Parameter(typeof(People), “x”);//创建一个x,类型为people
    //得到x.Id
    MemberExpression idParam = Expression.Field(peopleParam, “Id”);

    //得到ToString方法
    MethodInfo toStringWay = typeof(int).GetMethod(“ToString”, new Type[] { });

    //得到IndexOf的方法,然后new Type[]这个代表是得到参数为string的一个方法
    MethodInfo indexOfWay = typeof(string).GetMethod(“IndexOf”, new Type[] { typeof(string) });

    //通过下面方法得到x.Id.ToString()
    MethodCallExpression tostringResult = Expression.Call(idParam, toStringWay, new Expression[] { });

    //通过下面方法得到x.Id.ToString().IndexOf(“5”) ,MethodCallExpression继承于Expression
    MethodCallExpression indexOfResult = Expression.Call(tostringResult, indexOfWay, new Expression[] { Expression.Constant(“5”) });

    //x.Id.ToString().IndexOf(“5”)>=0
    var lambdaBody = Expression.GreaterThanOrEqual(indexOfResult, Expression.Constant(0));

    //得到x => x.Id.ToString().IndexOf(“5”) >= 0,后面的一个参数指的是x,如果有多个则指定多个
    Expression> lambdaResult = Expression.Lambda>(lambdaBody, new ParameterExpression[]
    { peopleParam });

    //通过lambdaResult.Compile()得到Func这样的委托,然后Invoke是调用委托
    bool result = lambdaResult.Compile().Invoke(new People() { Id = 155 });

    //普通的Lambda表达式
     Func<int,int,int> func = (x,y)=>  x + y - 2;
    //表达式目录树的Lambda表达式声明方式
    Expression<Func<int, int, int>> expression = (x, y) => x + y - 2;   
    
    //表达式目录树的拼接方式实现
    ParameterExpression parameterx =  Expression.Parameter(typeof(int), "x");
    ParameterExpression parametery =  Expression.Parameter(typeof(int), "y");
    ConstantExpression constantExpression = Expression.Constant(2, typeof(int));
    BinaryExpression binaryAdd = Expression.Add(parameterx, parametery);
    BinaryExpression binarySubtract = Expression.Subtract(binaryAdd, constantExpression);
    Expression<Func<int, int, int>> expressionMosaic = Expression.Lambda<Func<int, int, int>>(binarySubtract, new ParameterExpression[]
    {
           parameterx,
           parametery
    });
    
    int ResultLambda = func(5, 2);
    int ResultExpression = expression.Compile()(5, 2);
    int ResultMosaic = expressionMosaic.Compile()(5, 2);
    Console.WriteLine($"func:{ResultLambda}");
    Console.WriteLine($"expression:{ResultExpression}");
    Console.WriteLine($"expressionMosaic:{ResultMosaic}");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    pytorch实战---IMDB情感分析
    贴片加工炉温度曲线测试方法简介
    redis安装(单机模式和哨兵模式)
    智慧加油站视频监控行为识别分析系统
    Java岗大厂面试百日冲刺 - 日积月累,每日三题【Day5】 —— 基础篇2
    VS Code断点调式Cesium
    dolphinscheduler3.0beta搭建+hadoop+kerberos
    【Python21天学习挑战赛】-迭代器 & f-格式化 & 模块
    嵌入式工程师面试知识总结
    UE4采样纹理指定位置颜色
  • 原文地址:https://blog.csdn.net/u013400314/article/details/126750038