余于项目中逢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)); } } }