Expression 表达式树学习整理
整理了一下表达式树的一些东西,入门足够了
先从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));
今天就讲到这
最新文章
- onload事件-----addLoadEvent函数
- js 获取当前焦点所在的元素、给元素和input控件添加键盘监听事件、添加页面级的键盘监听事件
- java访问数据库的sql
- IE下Checkbox标签的onchange事件兼容
- [DevExpress]ChartControl之基准线示例
- C++ notes for beginners
- 345. Reverse Vowels of a String(C++)
- BZOJ1579 USACO 2009 Feb Gold 3.Revamping Trails Solution
- JDK-windows7环境变量配置-亲测版本 以及HelloWorld
- [C#]获得WindowsForm上所有特定类型的控件
- [偏序关系与CDQ分治]【学习笔记】
- SpringBoot整合RabbitMQ-整合演示
- Android进阶:七、Retrofit2.0原理解析之最简流程【下】
- sqlserver2017 SSAS配置远程访问不成功的问题
- DBS:TestSystem
- git reflog
- CAP二十年:“规则”变了
- System.Net.Http.Formatting的nuget版本冲突问题
- PHP-线程安全与非线程安全版本的区别
- 【转载】python import和from import
热门文章
- Oracle SQL Developer连接报错(ORA-12505)
- js日期时间函数
- 在 ServiceModel 客户端配置部分中,找不到引用协定“WebServiceTest.WebServiceSoap”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素
- Watchdog
- 23 其它话题 - 《Python 核心编程》
- 迅为iTOP-4412嵌入式开发板实现中断驱动例程
- Linux Bash shell one practice : array if else
- 怎么在ZBrush中通过遮罩得到子物体
- leetcode : valid binary search tree
- [cb] Assetbundle打包(一)