WYVE

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 1         static void TestExpression() {
 2  
 3             Expression<Func<string, string, bool>> expression = (x, y) => x.StartsWith(y);
 4             Console.WriteLine(expression.Compile()("First","Fir"));
 5 
 6             Console.WriteLine("-----");
 7 
 8 
 9             var arg1 = Expression.Parameter(typeof(string), "x");
10             var arg2 = Expression.Parameter(typeof(string), "y");
11             MethodInfo method = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });
12             Expression call = Expression.Call(arg1, method, arg2);
13             Console.WriteLine(call);//x.StartsWith(y)
14             var lamba = Expression.Lambda<Func<string, string, bool>>(call, new[] { arg1, arg2 });
15             Console.WriteLine(lamba);  //(x, y) => x.StartsWith(y)
16             var lambaCompiled = lamba.Compile();
17 
18             Console.WriteLine(lambaCompiled("First", "Fir"));
19             Console.WriteLine(lambaCompiled("First", "Second"));
20         }

 

posted on 2018-03-30 15:35  WYVE  阅读(170)  评论(0编辑  收藏  举报