1 // 第一题:画出下列表达式的表达式树。一开始,您很可能不知道某些操作其实也是表达式(比如取数组的运算符a[2]),不过没有关系,后面的习题将帮你验证这一点。
2
3 //-a
4 ParameterExpression e1 = Expression.Variable(typeof(int), "a");
5 UnaryExpression u = Expression.Negate(e1);//求反的表达式
6 Console.WriteLine(u);
7
8 //a + b * 2
9 ParameterExpression a = Expression.Variable(typeof(double), "a");
10 ParameterExpression b = Expression.Variable(typeof(double), "b");
11 ConstantExpression t = Expression.Constant(2d,typeof(double));
12 BinaryExpression cf = Expression.Multiply(b, t);
13 BinaryExpression jf = Expression.Add(a, cf);
14 Console.WriteLine(jf);
15
16 // Math.Sin(x) + Math.Cos(y)
17 ParameterExpression px = Expression.Parameter(typeof(double), "x");
18 MethodCallExpression sin = Expression.Call(null, typeof(Math).GetMethod("Sin", BindingFlags.Static | BindingFlags.Public), px);
19
20 ParameterExpression py = Expression.Parameter(typeof(double), "y");
21 MethodCallExpression cos = Expression.Call(null,
22 typeof(Math).GetMethod("Cos", BindingFlags.Public | BindingFlags.Static), py);
23
24 BinaryExpression jf = Expression.Add(sin, cos);
25
26 Console.WriteLine(jf.ToString());
27
28 // new StringBuilder(“Hello”)
29 ConstantExpression s = Expression.Constant("Hello", typeof(string));
30 NewExpression n = Expression.New(typeof(StringBuilder).GetConstructor(new Type[] { typeof(string) }), s);
31 Console.WriteLine(n.ToString());
32
33 // new int[] { a, b, a + b}
34 ParameterExpression a = Expression.Variable(typeof(int), "a");
35 ParameterExpression b = Expression.Variable(typeof(int), "b");
36 BinaryExpression c = Expression.Add(a, b);
37 NewArrayExpression n = Expression.NewArrayInit(typeof(int),a,b,c);
38 Console.WriteLine(n);
39 //a[i – 1] * i
40 ParameterExpression pi = Expression.Variable(typeof(int), "i");
41 ConstantExpression o = Expression.Constant(1, typeof(int));
42 BinaryExpression j = Expression.Subtract(pi, o);
43 ParameterExpression arr = Expression.Variable(typeof(int[]),"a");
44 IndexExpression t = Expression.ArrayAccess(arr, j);
45
46 BinaryExpression cf = Expression.Multiply(t, pi);
47 Console.WriteLine(cf);
48
49 a.Length > b | b >= 0
50 ParameterExpression a = Expression.Variable(typeof(string), "a");
51 MemberExpression m = Expression.Property(a, typeof(string).GetProperty("Length"));
52 ParameterExpression b = Expression.Variable(typeof(int), "b");
53 BinaryExpression g = Expression.GreaterThan(m, b);
54
55 ConstantExpression z = Expression.Constant(0, typeof(int));
56 BinaryExpression b2 = Expression.GreaterThanOrEqual(b, z);
57
58 BinaryExpression b3 = Expression.Or(g, b2);
59 Console.WriteLine(b3);
60
61 //(高难度)new System.Windows.Point() { X = Math.Sin(a), Y = Math.Cos(a) }
62 NewExpression n = Expression.New(typeof(System.Windows.Point));//new System.Windows.Point()
63 ParameterExpression a = Expression.Variable(typeof(double), "a");
64 MethodCallExpression sin = Expression.Call(null, typeof(Math).GetMethod("Sin"), a);
65 UnaryExpression sinD = Expression.Convert(sin, typeof(double));
66 MethodCallExpression cos = Expression.Call(null, typeof(Math).GetMethod("Cos"), a);
67 UnaryExpression cosD = Expression.Convert(cos, typeof(double));
68 MemberAssignment as1 = Expression.Bind(typeof(System.Windows.Point).GetProperty("X"), sinD);
69 MemberAssignment as2 = Expression.Bind(typeof(System.Windows.Point).GetProperty("Y"), cosD);
70
71 MemberInitExpression mm = Expression.MemberInit(n, as1, as2);
72
73 LambdaExpression le = Expression.Lambda(mm, a);
74
75 Console.WriteLine(le.Body);
76
77 //提示:注意运算符的优先级。倒数第二题的a是String类型,其余变量你可以用任意合适的简单类型。如果想知道以上表达式分别是什么表达式,可以查MSDN。