文章地址: http://www.cnblogs.com/Ninputer/archive/2009/08/28/expression_tree1.html
//定义一个表达式
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);
表达式树最终是一个内存中树状结构的数据。可以文本化,序列化、转存、传输等等。
课后习题:
//表达式求值时,验证表达式是否正确
LambdaExpression lambda = Expression.Lambda(expN, expA);//后面各题的表达式及参数传入此,
Delegate func = lambda.Compile();
MessageBox.Show(func.DynamicInvoke(10).ToString()); //调用时,注意参数要按顺序传入
//1、 -a
ParameterExpression expA = Expression.Parameter(typeof(double), "a");
UnaryExpression expN = Expression.Negate(expA);
//2、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);
//3、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);
//4、new StringBuilder("hello")
ConstantExpression exphello = Expression.Constant("hello");
NewExpression expNew = Expression.New(typeof(StringBuilder).GetConstructor(new Type[] { typeof(string) }), exphello);
之后这4个就不会手写了,根据说明,可以用直接写表达式字面量,然后反编译出表达式来。我用这方法反编译一下:
1、new int[] { a, b, a + b}
“Expression<Func<int, int, int[]>> exp = (a, b) => new int[] { a, b, a + b };“反编译的,还是自身!原作者说“不过C#编译器有时会使用一些作弊手法,聪明的你应该能找到绕过的手段……” ,我不知道是什么手段
2、a[i – 1] * i
“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 }
);
3、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 }
);
4、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) };” 稍加变形,反编译后还是自身!