9.3 表达式树

9.3.2 将表达式树编译成委托

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Expression firstArg = Expression.Constant(2);
 6             Expression secondArg = Expression.Constant(3);
 7             Expression add = Expression.Add(firstArg, secondArg);
 8 
 9             Func<int> compiled = Expression.Lambda<Func<int>>(add).Compile();
10             Console.WriteLine(compiled());
11 
12             Console.ReadKey();
13         }
14     }

 9.3.3 将C# Lambda表达式转换成表达式树

 1    class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Expression<Func<int>> return5 = () => 5;
 6             Func<int> compiled1 = return5.Compile();
 7             Console.WriteLine(compiled1());
 8 
 9             //=====================================
10 
11             Expression<Func<string, string, bool>> expression = (x, y) => x.StartsWith(y);
12             var compield2 = expression.Compile();
13             Console.WriteLine(compield2("First", "Second"));
14             Console.WriteLine(compield2("First", "Fir"));
15 
16             //=====================================
17 
18             MethodInfo method = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });
19             var target = Expression.Parameter(typeof(string), "x");
20             var methodArg = Expression.Parameter(typeof(string), "y");
21             Expression[] methodArgs = new[] { methodArg }; 
22 
23             Expression call = Expression.Call(target, method, methodArgs);
24 
25             var lambdaParameters = new[] { target, methodArg };
26             var lambda = Expression.Lambda<Func<string, string, bool>>(call, lambdaParameters);
27 
28             var compild3 = lambda.Compile();
29             Console.WriteLine(compild3("First", "Second"));
30             Console.WriteLine(compild3("First", "Fir"));
31 
32             Console.ReadKey();
33         }
34     }

 9.4.1 改变的起因:精简泛型方法调用

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             PrintConvert("I'm a string", x => x.Length);
 6 
 7             Console.ReadKey();
 8         }
 9         static void PrintConvert<TInput, TOutPut>(TInput inPut, Converter<TInput, TOutPut> convert)
10         {
11             Console.WriteLine(convert(inPut));
12         }
13     }

9.4.2 推断匿名函数的返回类型

 1     class Program
 2     {
 3         delegate T MyFunc<T>();
 4         static void Main(string[] args)
 5         {
 6             WriteResult(() => 5);
 7 
 8             WriteResult(delegate
 9             {
10                 if (DateTime.Now.Hour < 12)
11                 {
12                     return 10;
13                 }
14                 else
15                 {
16                     return new object();
17                 }
18             });
19             Console.ReadKey();
20         }
21         static void WriteResult<T>(MyFunc<T> func)
22         {
23             Console.WriteLine(func());
24         }
25     }

 9.4.3 分两个阶段进行的类型推断

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             ConvertTwice("I'm a string", x => x.Length, y => Math.Sqrt(y));
 6 
 7             Console.ReadKey();
 8         }
 9         static void ConvertTwice<TInput, TMiddle, TOutPut>(TInput inPut, Converter<TInput, TMiddle> firstConversion, Converter<TMiddle, TOutPut> secondConversion)
10         {
11             TMiddle middle = firstConversion(inPut);
12             TOutPut outPut = secondConversion(middle);
13             Console.WriteLine(outPut);
14         }
15     }

 9.4.4 选择正确的被重载的方法

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Write(1);
 6             Write(1d);
 7             Write((double)1);
 8 
 9             Console.ReadKey();
10             /*
11             int 1
12             double 1
13             double 1
14              */
15         }
16         static void Write(int x)
17         {
18             Console.WriteLine("int " + x);
19         }
20         static void Write(double x)
21         {
22             Console.WriteLine("double " + x);
23         }
24     }
原文地址:https://www.cnblogs.com/kikyoqiang/p/10050193.html