余于项目中逢Expression(表达式树),然今未明其用途也,记之以温。

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            ParameterExpression a = Expression.Parameter(typeof(int), "i");
            ParameterExpression b = Expression.Parameter(typeof(int), "j");
            BinaryExpression r1 = Expression.Multiply(a, b);

            ParameterExpression c = Expression.Parameter(typeof(int), "w");
            ParameterExpression d = Expression.Parameter(typeof(int), "x");
            BinaryExpression r2 = Expression.Multiply(c, d);

            BinaryExpression result = Expression.Add(r1, r2);
            Expression<Func<int, int, int, int, int>> lambda = Expression.Lambda<Func<int, int, int, int, int>>(result,a,b,c,d);

            Console.WriteLine(lambda);

            Func<int, int, int, int, int> f = lambda.Compile();
            Console.WriteLine(f(1,2,3,4));
        }

  
    }
}

 

posted on 2018-01-22 16:27  chester·chen  阅读(138)  评论(0编辑  收藏  举报