2 using System.Collections.Generic;
4 using System.Linq.Expressions;
8 static void AssertNodeType (LambdaExpression e, ExpressionType et)
10 if (e.Body.NodeType != et)
11 throw new ApplicationException (e.Body.NodeType + " != " + et);
14 static void Assert<T> (T expected, T value)
16 if (!EqualityComparer<T>.Default.Equals (expected, value))
17 throw new ApplicationException (expected + " != " + value);
22 Expression<Func<int, int, int>> e = (int a, int b) => a + b;
23 AssertNodeType (e, ExpressionType.Add);
24 Assert (50, e.Compile ().Invoke (20, 30));
26 Expression<Func<int?, int?, int?>> e2 = (a, b) => a + b;
27 AssertNodeType (e2, ExpressionType.Add);
28 Assert (null, e2.Compile ().Invoke (null, 3));
31 void AddCheckedTest ()
34 Expression<Func<int, int, int>> e = (int a, int b) => a + b;
36 AssertNodeType (e, ExpressionType.AddChecked);
37 Assert (-10, e.Compile ().Invoke (20, -30));
43 Expression<Func<bool, bool, bool>> e = (bool a, bool b) => a & b;
45 AssertNodeType (e, ExpressionType.And);
46 Assert (false, e.Compile ().Invoke (false, true));
51 Expression<Func<bool, bool, bool>> e = (bool a, bool b) => a && b;
53 AssertNodeType (e, ExpressionType.AndAlso);
54 Assert (false, e.Compile ().Invoke (true, false));
57 void ArrayIndexTest ()
59 Expression<Func<string[], long, string>> e = (string[] a, long i) => a [i];
60 AssertNodeType (e, ExpressionType.ArrayIndex);
61 Assert ("b", e.Compile ().Invoke (new string [] { "a", "b", "c" }, 1));
63 Expression<Func<string [], string>> e2 = (string [] a) => a [0];
64 AssertNodeType (e2, ExpressionType.ArrayIndex);
65 Assert ("a", e2.Compile ().Invoke (new string [] { "a", "b" }));
67 Expression<Func<object [,], int, int, object>> e3 = (object [,] a, int i, int j) => a [i, j];
68 AssertNodeType (e3, ExpressionType.Call);
70 Assert ("z", e3.Compile ().Invoke (
71 new object [,] { { 1, 2 }, { "x", "z" } }, 1, 1));
73 Expression<Func<decimal [][], byte, decimal>> e4 = (decimal [][] a, byte b) => a [b][1];
74 AssertNodeType (e4, ExpressionType.ArrayIndex);
76 decimal [] [] array = { new decimal [] { 1, 9 }, new decimal [] { 10, 90 } };
77 Assert (90, e4.Compile ().Invoke (array, 1));
80 void ArrayLengthTest ()
82 int o = new int [0].Length;
84 Expression<Func<double [], int>> e = (double [] a) => a.Length;
85 AssertNodeType (e, ExpressionType.ArrayLength);
86 Assert (0, e.Compile ().Invoke (new double [0]));
87 Assert (9, e.Compile ().Invoke (new double [9]));
90 //Expression<Func<string [,], int>> e2 = (string [,] a) => a.Length;
91 //AssertNodeType (e2, ExpressionType.MemberAccess);
92 //Assert (0, e2.Compile ().Invoke (new string [0, 0]));
97 Expression<Func<int, int>> e = (int a) => Math.Max (a, 5);
98 AssertNodeType (e, ExpressionType.Call);
99 Assert (5, e.Compile ().Invoke (2));
100 Assert (9, e.Compile ().Invoke (9));
102 Expression<Func<string, string>> e2 = (string a) => InstanceMethod (a);
103 AssertNodeType (e2, ExpressionType.Call);
104 Assert ("abc", e2.Compile ().Invoke ("abc"));
107 Expression<Func<int, string, int, object>> e3 = (int index, string a, int b) => InstanceParamsMethod (index, a, b);
108 AssertNodeType (e3, ExpressionType.Call);
109 Assert<object> (4, e3.Compile ().Invoke (1, "a", 4));
111 Expression<Func<object>> e4 = () => InstanceParamsMethod (0);
112 AssertNodeType (e4, ExpressionType.Call);
113 Assert<object> ("<empty>", e4.Compile ().Invoke ());
116 Expression<Func<int, int>> e5 = (int a) => GenericMethod (a);
117 AssertNodeType (e5, ExpressionType.Call);
118 Assert (5, e5.Compile ().Invoke (5));
125 string InstanceMethod (string arg)
130 object InstanceParamsMethod (int index, params object[] args)
134 if (args.Length == 0)
139 T GenericMethod<T> (T t)
145 public static int Main ()
147 Tester e = new Tester ();