1 // 第一题:画出下列表达式的表达式树。一开始,您很可能不知道某些操作其实也是表达式(比如取数组的运算符a[2]),不过没有关系,后面的习题将帮你验证这一点。 //-a
ParameterExpression e1 = Expression.Variable(typeof(int), "a");
UnaryExpression u = Expression.Negate(e1);//求反的表达式
Console.WriteLine(u); //a + b * 2
ParameterExpression a = Expression.Variable(typeof(double), "a");
ParameterExpression b = Expression.Variable(typeof(double), "b");
ConstantExpression t = Expression.Constant(2d,typeof(double));
BinaryExpression cf = Expression.Multiply(b, t);
BinaryExpression jf = Expression.Add(a, cf);
Console.WriteLine(jf); // Math.Sin(x) + Math.Cos(y)
ParameterExpression px = Expression.Parameter(typeof(double), "x");
MethodCallExpression sin = Expression.Call(null, typeof(Math).GetMethod("Sin", BindingFlags.Static | BindingFlags.Public), px); ParameterExpression py = Expression.Parameter(typeof(double), "y");
MethodCallExpression cos = Expression.Call(null,
typeof(Math).GetMethod("Cos", BindingFlags.Public | BindingFlags.Static), py); BinaryExpression jf = Expression.Add(sin, cos); Console.WriteLine(jf.ToString()); // new StringBuilder(“Hello”)
ConstantExpression s = Expression.Constant("Hello", typeof(string));
NewExpression n = Expression.New(typeof(StringBuilder).GetConstructor(new Type[] { typeof(string) }), s);
Console.WriteLine(n.ToString()); // new int[] { a, b, a + b}
ParameterExpression a = Expression.Variable(typeof(int), "a");
ParameterExpression b = Expression.Variable(typeof(int), "b");
BinaryExpression c = Expression.Add(a, b);
NewArrayExpression n = Expression.NewArrayInit(typeof(int),a,b,c);
Console.WriteLine(n);
//a[i – 1] * i
ParameterExpression pi = Expression.Variable(typeof(int), "i");
ConstantExpression o = Expression.Constant(, typeof(int));
BinaryExpression j = Expression.Subtract(pi, o);
ParameterExpression arr = Expression.Variable(typeof(int[]),"a");
IndexExpression t = Expression.ArrayAccess(arr, j); BinaryExpression cf = Expression.Multiply(t, pi);
Console.WriteLine(cf); a.Length > b | b >=
ParameterExpression a = Expression.Variable(typeof(string), "a");
MemberExpression m = Expression.Property(a, typeof(string).GetProperty("Length"));
ParameterExpression b = Expression.Variable(typeof(int), "b");
BinaryExpression g = Expression.GreaterThan(m, b); ConstantExpression z = Expression.Constant(, typeof(int));
BinaryExpression b2 = Expression.GreaterThanOrEqual(b, z); BinaryExpression b3 = Expression.Or(g, b2);
Console.WriteLine(b3); //(高难度)new System.Windows.Point() { X = Math.Sin(a), Y = Math.Cos(a) }
NewExpression n = Expression.New(typeof(System.Windows.Point));//new System.Windows.Point()
ParameterExpression a = Expression.Variable(typeof(double), "a");
MethodCallExpression sin = Expression.Call(null, typeof(Math).GetMethod("Sin"), a);
UnaryExpression sinD = Expression.Convert(sin, typeof(double));
MethodCallExpression cos = Expression.Call(null, typeof(Math).GetMethod("Cos"), a);
UnaryExpression cosD = Expression.Convert(cos, typeof(double));
MemberAssignment as1 = Expression.Bind(typeof(System.Windows.Point).GetProperty("X"), sinD);
MemberAssignment as2 = Expression.Bind(typeof(System.Windows.Point).GetProperty("Y"), cosD); MemberInitExpression mm = Expression.MemberInit(n, as1, as2); LambdaExpression le = Expression.Lambda(mm, a); Console.WriteLine(le.Body); //提示:注意运算符的优先级。倒数第二题的a是String类型,其余变量你可以用任意合适的简单类型。如果想知道以上表达式分别是什么表达式,可以查MSDN。