Linq 表达式树

    class Program
    {
        static void Main(string[] args)
        {
            Expression<Func<int, int, int>> ex = (x, y) => x + y;
            
            Console.Read();
        }
    }

从图中可以看到表达式树最重要的就是Body,Left,Right,NodeType(当前节点类型)

我们可以这样调用

 

当然上面的ex还不够灵活,可以用下面的代码替换

            var x1 = Expression.Parameter(typeof(int), "x");
            var y1 = Expression.Parameter(typeof(int), "x");
            var body = Expression.Add(x1, y1);
            var ex1 = Expression.Lambda<Func<int,int,int>>(body, new ParameterExpression[] { x1, y1 }).Compile();
            Console.WriteLine(ex1(2,2));

Hold on, everything is possible.
原文地址:https://www.cnblogs.com/student-note/p/6741703.html