(转)Expression 表达式树学习整理

原文地址:http://www.cnblogs.com/li-peng/p/3154381.html

 

整理了一下表达式树的一些东西,入门足够了

先从ConstantExpression 开始一步一步的来吧  它表示具有常量值的表达式

我们选建一个控制台应用程序

?
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你也许不知道是什么回事,不要急我下边会详细讲的,这相当于

Console.WriteLine("aaa");  输出一个常量,看一下结果

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

?
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!!

有点感觉了吧,慢慢来好玩的还在后边,现在我们就说一下MethodCallExpression它可以调用静态方法和实例方法,我们上边的代码就是调用 的静态方法

,我先讲一下调用静态方法,再讲调用实例方法。

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

?
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));

  

今天就讲到这

转载于:https://www.cnblogs.com/fcsh820/p/3155761.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值