01.创建一个Console.WriteLine()

先从ConstantExpression 开始一步一步的来吧 它表示具有常量值的表达式,我们选建一个控制台应用程序

  1. //1.1此处的目的在于创建一个Console.WreiteLine();
  2. ConstantExpression _constantExp = Expression.Constant("testDmoe", typeof(string));//一个常量
  3. MethodCallExpression _methodCallExp = Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), _constantExp);
  4. System.Linq.Expressions.Expression<Action> consoleLambdaExp = Expression.Lambda<Action>(_methodCallExp);
  5. consoleLambdaExp.Compile()();

下边的MethodCallExpression你也许不知道是什么回事,不要急我下边会详细讲的,这相当于
Console.WriteLine(“aaa”); 输出一个常量,看一下结果
P0:Expression 表达树 - 图1

02.添加一个参数

如果想自己输入一个值输出呢,那就用ParameterExpression 它表示一个参数表达式,我们只要把上边的代码做一下小改动就行

  1. //1.2新增一个参数功能
  2. ParameterExpression _parameterExp = Expression.Parameter(typeof(string), "MyParameter");
  3. MethodCallExpression _methodCallExpp = Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), _parameterExp);
  4. Expression<Action<string>> _consStringExp = Expression.Lambda<Action<string>>(_methodCallExpp, _parameterExp);
  5. _consStringExp.Compile()("Hello!!");

参数parameExp就是一个string类型的变量我们让它输出一个Hello!!
P0:Expression 表达树 - 图2

03.调用静态方法

我们建一个返回string的静态方法,传入一个object类型的值

  1. public static string SomeMethodNo1(object str)
  2. {
  3. string _str = str + " from SomeMethodNo1 test static method.";
  4. Console.WriteLine(_str);
  5. return _str;
  6. }
  7. //2.1调用静态方法
  8. ParameterExpression _parameterExpObj1 = Expression.Parameter(typeof(object), "objPara");
  9. MethodCallExpression _methodCallExp1 = Expression.Call(typeof(Program).GetMethod("SomeMethodNo1", new Type[] { typeof(object) }), _parameterExpObj1);
  10. Expression<Func<object, string>> _myLambdaState = Expression.Lambda<Func<object, string>>(_methodCallExp1, _parameterExpObj1);
  11. string s_tr = _myLambdaState.Compile()("Content1:");
  12. Console.WriteLine("返回值: " + s_tr);

new Type[] { typeof(object) } 就是我们的方法里的参数类型,后边的paraObj是相当于参数值了,如果 是多参数就在 Type[],和后边再加上相应 的类型和参数就行

04.调用实例方法

  1. public string SomeMethodNo2(object str)
  2. {
  3. string _str = str + " from SomeMethodNo2 test instance method.";
  4. Console.WriteLine(_str);
  5. return _str;
  6. }
  7. //2.2调用实例
  8. Program _program = new Program();
  9. ParameterExpression _parameterExpObj2 = Expression.Parameter(typeof(object),"objParameter");
  10. MethodCallExpression _methodCallExp2 = Expression.Call(Expression.Constant(_program), typeof(Program).GetMethod("SomeMethodNo2"), _parameterExpObj2);
  11. Expression<Func<object,string>> _myLambdaState2 = Expression.Lambda<Func<object, string>>(_methodCallExp2, _parameterExpObj2);
  12. string str2 = _myLambdaState2.Compile()("Content2:");
  13. Console.WriteLine("返回值: "+str2);

05.UnaryExpression一元运算符表达式

  1. //3.1 一元运算符表达式 UnaryExpression 减法
  2. ConstantExpression _consNum = Expression.Constant(5, typeof(int));
  3. UnaryExpression _unaryPlus = Expression.Decrement(_consNum);
  4. Expression<Func<int>> _unaryLam = Expression.Lambda<Func<int>>(_unaryPlus);
  5. Console.WriteLine(_unaryLam.Compile()());

06.BinaryExpression 二元运算符表达式

  1. //3.2 二元运算符表达式 BinaryExpression 加法
  2. ParameterExpression _ParaA = Expression.Parameter(typeof(int), "a");
  3. ParameterExpression _ParaB = Expression.Parameter(typeof(int), "b");
  4. BinaryExpression _BinaAdd = Expression.Add(_ParaA, _ParaB);
  5. Expression<Func<int, int, int>> _MyBinaryAddLamb = Expression.Lambda<Func<int, int, int>>(_BinaAdd, new ParameterExpression[] { _ParaA, _ParaB });
  6. Console.WriteLine("表达式: " + _MyBinaryAddLamb);
  7. Console.WriteLine(_MyBinaryAddLamb.Compile()(3, 6));

07.我们做一把两个表达式放一起做一个例子吧 (a+b)*(—c)

  1. //3.3我们做一把两个表达式放一起做一个例子吧 (a+b)*(--c)
  2. ParameterExpression _ParaA = Expression.Parameter(typeof(int), "a");
  3. ParameterExpression _ParaB = Expression.Parameter(typeof(int), "b");
  4. BinaryExpression _BinaAdd = Expression.Add(_ParaA, _ParaB); //a+b
  5. ParameterExpression _paraC = Expression.Parameter(typeof(int), "c");
  6. UnaryExpression _paraDecr = Expression.Decrement(_paraC); //(a+b)*(--c)
  7. BinaryExpression _binaMultiply = Expression.Multiply(_BinaAdd, _paraDecr);
  8. Expression<Func<int, int, int, int>> _MyBinaryLamb = Expression.Lambda<Func<int, int, int, int>>(_binaMultiply, new ParameterExpression[] { _ParaA, _ParaB, _paraC });
  9. Console.WriteLine("表达式: " + _MyBinaryLamb);
  10. Console.WriteLine(_MyBinaryLamb.Compile()(3, 6, 5));

Total Result

image.png