文章地址: http://www.cnblogs.com/Ninputer/archive/2009/08/28/expression_tree1.htmlhtml
//定义一个表达式 ConstantExpression exp1 = Expression.Constant(1); ConstantExpression exp2 = Expression.Constant(2); BinaryExpression exp12 = Expression.Add(exp1, exp2); ConstantExpression exp3 = Expression.Constant(3); BinaryExpression exp123 = Expression.Add(exp12, exp3); //须要转为LambdaExpression 后,才能执行 LambdaExpression lambda = Expression.Lambda(exp123); Delegate func = lambda.Compile(); MessageBox.Show( func.DynamicInvoke(null).ToString()); //直接定义LambdaExpression ParameterExpression expA=Expression.Parameter(typeof(double),"a"); MethodCallExpression expCall = Expression.Call(null, typeof(Math).GetMethod("Sin"), expA); lambda = Expression.Lambda(expCall, expA); //这个是表达式字面量,自动从语言层面的表达式转为表达式树。 这个特殊语法只适于Lambda表达式。是一种语法糖! Expression<Func<double, double>> exp = a => Math.Sin(a);
表达式树最终是一个内存中树状结构的数据。能够文本化,序列化、转存、传输等等。
web
运行时分析表达式的逻辑express
序列化或者传输表达式c#
从新编译成可执行的代码数组
课后习题:ui
//表达式求值时,验证表达式是否正确 LambdaExpression lambda = Expression.Lambda(expN, expA);//后面各题的表达式及参数传入此, Delegate func = lambda.Compile(); MessageBox.Show(func.DynamicInvoke(10).ToString()); //调用时,注意参数要按顺序传入 //一、 -a ParameterExpression expA = Expression.Parameter(typeof(double), "a"); UnaryExpression expN = Expression.Negate(expA); //二、a+b*2 ParameterExpression expA = Expression.Parameter(typeof(double), "a"); ParameterExpression expB = Expression.Parameter(typeof(double), "b"); ConstantExpression exp2=Expression.Constant(2.0); BinaryExpression expb2 = Expression.Multiply(exp2, expB); BinaryExpression expab2 = Expression.Add(expA, expb2); //三、Math.Sin(x)+Math.Cos(y) ParameterExpression expx = Expression.Parameter(typeof(double), "x"); ParameterExpression expy = Expression.Parameter(typeof(double), "y"); MethodCallExpression expsin = Expression.Call(null, typeof(Math).GetMethod("Sin"), expx); MethodCallExpression expcos = Expression.Call(null, typeof(Math).GetMethod("Cos"), expy); BinaryExpression expAdd = Expression.Add(expsin, expcos); //四、new StringBuilder("hello") ConstantExpression exphello = Expression.Constant("hello"); NewExpression expNew = Expression.New(typeof(StringBuilder).GetConstructor(new Type[] { typeof(string) }), exphello);
以后这4个就不会手写了,根听说明,能够用直接写表达式字面量,而后反编译出表达式来。我用这方法反编译一下:spa
一、new int[] { a, b, a + b}code
“Expression<Func<int, int, int[]>> exp = (a, b) => new int[] { a, b, a + b };“反编译的,仍是自身!原做者说“不过C#编译器有时会使用一些做弊手法,聪明的你应该能找到绕过的手段……” ,我不知道是什么手段orm
二、a[i – 1] * ihtm
“Expression<Func<int[], int,int>> exp = (a, i) => a[i-1]*i;”反编译为:
Expression.Lambda<Func<int[], int, int>>( Expression.Multiply( Expression.ArrayIndex( //注意这用的方法 c1 = Expression.Parameter(typeof(int[]), "a"), Expression.Subtract( c2 = Expression.Parameter(typeof(int), "i"), Expression.Constant(1, typeof(int)) ) ), c2), new ParameterExpression[] { c1, c2 } );
三、a.Length > b | b >= 0
“Expression<Func<int[], int, bool>> exp = (a, b) => a.Length > b || b > 0;”反编译为:
ParameterExpression c1; ParameterExpression c2; //注意如何获取数组长度。我本身写时,试着: MemberExpression expLen = Expression.Field(expA, "Length") //结果是不对的。代表length不是一个普通字段 。 Expression<Func<int[], int, bool>> exp = Expression.Lambda<Func<int[], int, bool>>( Expression.OrElse( Expression.GreaterThan(Expression.ArrayLength(c1 = Expression.Parameter(typeof(int[]), "a")),c2 = Expression.Parameter(typeof(int), "b")), Expression.GreaterThan(c2, Expression.Constant(0, typeof(int))) ), new ParameterExpression[] { c1, c2 } );
四、new System.Windows.Point() { X = Math.Sin(a), Y = Math.Cos(a) }
“Expression<Func<double, Point>> exp = (a) => new Point() { X = (int)Math.Sin(a), Y = (int)Math.Cos(a) };” 稍加变形,反编译后仍是自身!