C# 表达式树demo

class Program
{
    static void Main(string[] args)
    {
        //创建Expression参数
        var left = System.Linq.Expressions.Expression.Parameter(typeof(int), "a");
        var right = System.Linq.Expressions.Expression.Parameter(typeof(int), "b");
        //创建body
        //模拟 (x,y)=>{return x+y;};
        var body = System.Linq.Expressions.Expression.Add(left, right);
        var lamda = System.Linq.Expressions.Expression.Lambda<Func<int, int, int>>(body, left, right).Compile();
        var result = lamda(60, 0);
        Console.WriteLine(result);
        Console.ReadKey();
    }
}

相加操作,结果:60

posted @ 2016-01-12 18:03  b̶i̶n̶g̶.̶  阅读(227)  评论(0编辑  收藏  举报