读完脑袋大哥的文章Expression Tree上手指南(一)
做习题
-a
a + b * 2
Math.Sin(x) + Math.Cos(y)
new StringBuilder(“Hello”)
new int[] { a, b, a + b}
a[i – 1] * i
a.Length > b | b >= 0
new System.Windows.Point() { X = Math.Sin(a), Y = Math.Cos(a) }
class Program { static void Main(string[] args) { Method1(); Method2(); Method3(); Method4(); Method5(); Method6(); Method7(); Method8(); Console.ReadLine(); } public static void Method1() { //-a ParameterExpression expA = Expression.Parameter(typeof(int), "a"); //参数a var express = Expression.Negate(expA); Console.WriteLine(express); } public static void Method2() { //a+b*2 ParameterExpression expA = Expression.Parameter(typeof(int), "a"); //参数a ParameterExpression expB = Expression.Parameter(typeof(int), "b"); //参数a var express = Expression.Add(Expression.Multiply(expB, Expression.Constant(2)), expA); Console.WriteLine(express); } public static void Method3() { //Math.Sin(x) + Math.Cos(y) ParameterExpression expX = Expression.Parameter(typeof(double), "x"); //参数x ParameterExpression expY = Expression.Parameter(typeof(double), "y"); //参数y var x = Expression.Call(null, typeof(Math).GetMethod("Sin", BindingFlags.Static | BindingFlags.Public), expX); var y = Expression.Call(null, typeof(Math).GetMethod("Cos", BindingFlags.Static | BindingFlags.Public), expY); Console.WriteLine(Expression.Add(x, y)); ; } public static void Method4() { var d = Expression.Constant("abc", typeof(string)); Type[] types = new Type[1]; types[0] = typeof(string); Console.WriteLine(Expression.New(typeof(StringBuilder).GetConstructor(types), d)); } public static void Method5() { //new int[] { a, b, a + b} List<System.Linq.Expressions.Expression> trees = new List<System.Linq.Expressions.Expression> { Expression.Parameter(typeof(int), "a"), Expression.Parameter(typeof(int), "b"), Expression.Add(Expression.Parameter(typeof(int), "a"), Expression.Parameter(typeof(int), "b")) }; Console.WriteLine(Expression.NewArrayInit(typeof(int), trees)); } public static void Method6() { //a[i – 1] * i var i = Expression.Parameter(typeof(int), "i"); var a = Expression.Parameter(typeof(int[]), "a"); var express = Expression.Multiply(Expression.ArrayIndex(a, Expression.Subtract(i, Expression.Constant(1))), i); Console.WriteLine(express); } public static void Method7() { //a.Length > b | b >= 0 var a = Expression.Parameter(typeof(int[]), "a"); var b = Expression.Parameter(typeof(int), "b"); Console.WriteLine(Expression.Or(Expression.GreaterThan(Expression.ArrayLength(a), b), Expression.GreaterThanOrEqual(b, Expression.Constant(0)))); } public static void Method8() { //new System.Windows.Point() { X = Math.Sin(a), Y = Math.Cos(a) } ParameterExpression a = Expression.Parameter(typeof(double), "a"); var express = Expression.MemberInit(Expression.New(typeof(System.Windows.Point)), new MemberBinding[] { Expression.Bind(typeof(System.Windows.Point).GetProperty("X"), Expression.Call(null, typeof(Math).GetMethod("Sin"), a)), Expression.Bind(typeof(System.Windows.Point).GetProperty("Y"), Expression.Call(null, typeof(Math).GetMethod("Cos"), a)) } ); Console.WriteLine(express); } }
不知是否正确,欢迎拍砖
运行界面如下: