01.创建一个Console.WriteLine()
先从ConstantExpression 开始一步一步的来吧 它表示具有常量值的表达式,我们选建一个控制台应用程序
//1.1此处的目的在于创建一个Console.WreiteLine();
ConstantExpression _constantExp = Expression.Constant("testDmoe", typeof(string));//一个常量
MethodCallExpression _methodCallExp = Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), _constantExp);
System.Linq.Expressions.Expression<Action> consoleLambdaExp = Expression.Lambda<Action>(_methodCallExp);
consoleLambdaExp.Compile()();
下边的MethodCallExpression你也许不知道是什么回事,不要急我下边会详细讲的,这相当于
Console.WriteLine(“aaa”); 输出一个常量,看一下结果
02.添加一个参数
如果想自己输入一个值输出呢,那就用ParameterExpression 它表示一个参数表达式,我们只要把上边的代码做一下小改动就行
//1.2新增一个参数功能
ParameterExpression _parameterExp = Expression.Parameter(typeof(string), "MyParameter");
MethodCallExpression _methodCallExpp = Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), _parameterExp);
Expression<Action<string>> _consStringExp = Expression.Lambda<Action<string>>(_methodCallExpp, _parameterExp);
_consStringExp.Compile()("Hello!!");
参数parameExp就是一个string类型的变量我们让它输出一个Hello!!
03.调用静态方法
我们建一个返回string的静态方法,传入一个object类型的值
public static string SomeMethodNo1(object str)
{
string _str = str + " from SomeMethodNo1 test static method.";
Console.WriteLine(_str);
return _str;
}
//2.1调用静态方法
ParameterExpression _parameterExpObj1 = Expression.Parameter(typeof(object), "objPara");
MethodCallExpression _methodCallExp1 = Expression.Call(typeof(Program).GetMethod("SomeMethodNo1", new Type[] { typeof(object) }), _parameterExpObj1);
Expression<Func<object, string>> _myLambdaState = Expression.Lambda<Func<object, string>>(_methodCallExp1, _parameterExpObj1);
string s_tr = _myLambdaState.Compile()("Content1:");
Console.WriteLine("返回值: " + s_tr);
new Type[] { typeof(object) } 就是我们的方法里的参数类型,后边的paraObj是相当于参数值了,如果 是多参数就在 Type[],和后边再加上相应 的类型和参数就行
04.调用实例方法
public string SomeMethodNo2(object str)
{
string _str = str + " from SomeMethodNo2 test instance method.";
Console.WriteLine(_str);
return _str;
}
//2.2调用实例
Program _program = new Program();
ParameterExpression _parameterExpObj2 = Expression.Parameter(typeof(object),"objParameter");
MethodCallExpression _methodCallExp2 = Expression.Call(Expression.Constant(_program), typeof(Program).GetMethod("SomeMethodNo2"), _parameterExpObj2);
Expression<Func<object,string>> _myLambdaState2 = Expression.Lambda<Func<object, string>>(_methodCallExp2, _parameterExpObj2);
string str2 = _myLambdaState2.Compile()("Content2:");
Console.WriteLine("返回值: "+str2);
05.UnaryExpression一元运算符表达式
//3.1 一元运算符表达式 UnaryExpression 减法
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()());
06.BinaryExpression 二元运算符表达式
//3.2 二元运算符表达式 BinaryExpression 加法
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));
07.我们做一把两个表达式放一起做一个例子吧 (a+b)*(—c)
//3.3我们做一把两个表达式放一起做一个例子吧 (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));