整理了一下表达式树的一些东西,入门足够了spa
先从ConstantExpression 开始一步一步的来吧 它表示具备常量值的表达式blog
咱们选建一个控制台应用程序ip
ConstantExpression _constExp = Expression.Constant("aaa",typeof(string));//一个常量 //Console.Writeline("aaa"); MethodCallExpression _methodCallexp=Expression.Call(typeof(Console).GetMethod("WriteLine",new Type[]{typeof(string)}),_constExp); Expression<Action> consoleLambdaExp = Expression.Lambda<Action>(_methodCallexp); consoleLambdaExp.Compile()(); Console.ReadLine();
下边的MethodCallExpression你也许不知道是什么回事,不要急我下边会详细讲的,这至关于rem
Console.WriteLine("aaa"); 输出一个常量,看一下结果string
若是想本身输入一个值输出呢,那就用ParameterExpression 它表示一个参数表达式,咱们只要把上边的代码作一下小改动就行it
ParameterExpression _parameExp = Expression.Parameter(typeof(string), "MyParameter"); MethodCallExpression _methodCallexpP = Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), _parameExp); Expression<Action<string>> _consStringExp = Expression.Lambda<Action<string>>(_methodCallexpP, _parameExp); _consStringExp.Compile()("Hello!!");
参数parameExp就是一个string类型的变量咱们让它输出一个Hello!!io
有点感受了吧,慢慢来好玩的还在后边,如今咱们就说一下MethodCallExpression它能够调用静态方法和实例方法,咱们上边的代码就是调用 的静态方法console
,我先讲一下调用静态方法,再讲调用实例方法。入门
咱们建一个返回string的静态方法,传入一个object类型的值class
public static string ConsStr(object str) { string _str = str + "aa"; Console.WriteLine(_str); return _str; }
看一下咱们是怎么调用本身的静态方法的
ParameterExpression _paraObj = Expression.Parameter(typeof(object), "objPara"); MethodCallExpression _MyStateMethod = Expression.Call(typeof(Program).GetMethod("ConsStr", new Type[] { typeof(object) }), _paraObj); Expression<Func<object, string>> _meyLambdaState = Expression.Lambda<Func<object, string>>(_MyStateMethod, _paraObj); string s_tr = _meyLambdaState.Compile()("ni Hao"); Console.WriteLine("返回值: " + s_tr);
new Type[] { typeof(object) } 就是咱们的方法里的参数类型,后边的paraObj是至关于参数值了,若是 是多参数就在 Type[],和后边再加上相应 的类型和参数就行
静态方法你有些了解了,下面讲一下调用实例方法
咱们写一个非静态方法
public string ConsStr2(object str) { string _str = str + "aa"; Console.WriteLine(_str); return _str; }
调用的时候只要把上边的代码改动一点就ok Expression.Call为咱们提供了咱们想要的重载
Program _pg = new Program(); ParameterExpression _paraObj2 = Expression.Parameter(typeof(object), "objPara"); MethodCallExpression _MyStateMethod2 = Expression.Call(Expression.Constant(_pg), typeof(Program).GetMethod("ConsStr2"), _paraObj2); Expression<Func<object, string>> _meyLambdaState2 = Expression.Lambda<Func<object, string>>(_MyStateMethod2, _paraObj2); string s_tr2 = _meyLambdaState.Compile()("you shi ni "); Console.WriteLine("返回值: " + s_tr2);
简单吧。
再下来咱们讲什么呢,也许你猜到了UnaryExpression一元运算符表达式和 BinaryExpression 二元运算符表达式
咱们先看一个这两个表达式的简单例子后,咱们再作一个复杂的例子
UnaryExpression咱们作一个5--的表达式
ConstantExpression _consNum = Expression.Constant(5, typeof(int)); UnaryExpression _unaryPlus = Expression.Decrement(_consNum); Expression<Func<int>> _unaryLam = Expression.Lambda<Func<int>>(_unaryPlus); Console.WriteLine(_unaryLam.Compile()());
BinaryExpression 咱们作一个a+b的例子
ParameterExpression _ParaA = Expression.Parameter(typeof(int), "a"); ParameterExpression _ParaB = Expression.Parameter(typeof(int), "b"); BinaryExpression _BinaAdd = Expression.Add(_ParaA, _ParaB); Expression<Func<int, int, int>> _MyBinaryAddLamb = Expression.Lambda<Func<int, int, int>>(_BinaAdd, new ParameterExpression[] { _ParaA, _ParaB }); Console.WriteLine("表达式: "+ _MyBinaryAddLamb); Console.WriteLine(_MyBinaryAddLamb.Compile()(3, 6));
不难吧,
咱们作一把两个表达式放一块儿作一个例子吧 (a+b)*(--c)
ParameterExpression _ParaA = Expression.Parameter(typeof(int), "a"); ParameterExpression _ParaB = Expression.Parameter(typeof(int), "b"); BinaryExpression _BinaAdd = Expression.Add(_ParaA, _ParaB); //a+b ParameterExpression _paraC = Expression.Parameter(typeof(int), "c"); UnaryExpression _paraDecr = Expression.Decrement(_paraC); //(a+b)*(--c) BinaryExpression _binaMultiply = Expression.Multiply(_BinaAdd, _paraDecr); Expression<Func<int, int, int, int>> _MyBinaryLamb = Expression.Lambda<Func<int, int, int, int>>(_binaMultiply, new ParameterExpression[] { _ParaA, _ParaB, _paraC }); Console.WriteLine("表达式: "+ _MyBinaryLamb); Console.WriteLine(_MyBinaryLamb.Compile()(3, 6, 5));
今天就讲到这