C#秘密武器之表达式树
一、表达式树入门
Lambda表达式树很复杂,从概念上很难理解清楚,一句话,表达式树是一种数据结构!这里我们通过下面的这个例子来理解一下表达式树,你就能看个大概:
lambda表达式树动态创建方法
static void Main(string[] args)
{
//i*j+w*x
ParameterExpression a = Expression.Parameter(typeof(int),"i"); //创建一个表达式树中的参数,作为一个节点,这里是最下层的节点
ParameterExpression b = Expression.Parameter(typeof(int),"j");
BinaryExpression r1 = Expression.Multiply(a,b); //这里i*j,生成表达式树中的一个节点,比上面节点高一级 ParameterExpression c = Expression.Parameter(typeof(int), "w");
ParameterExpression d = Expression.Parameter(typeof(int), "x");
BinaryExpression r2 = Expression.Multiply(c, d); BinaryExpression result = Expression.Add(r1,r2); //运算两个中级节点,产生终结点 Expression<Func<int, int, int, int, int>> lambda = Expression.Lambda<Func<int, int, int, int, int>>(result,a,b,c,d); Console.WriteLine(lambda + ""); //输出‘(i,j,w,x)=>((i*j)+(w*x))’,z对应参数b,p对应参数a Func<int, int, int, int, int> f= lambda.Compile(); //将表达式树描述的lambda表达式,编译为可执行代码,并生成该lambda表达式的委托; Console.WriteLine(f(, , , ) + ""); //输出结果2
Console.ReadKey();
}
以上代码构成的Lambda表达式树如下图:
二、常见的一些表达式树用法
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();
输出一个常量,看一下结果
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!!");
输出结果:
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);
输出结果:
MethodCallExpression调用实例方法
我们写一个非静态方法
public string ConsStr2(object str)
{
string _str = str + "aa";
Console.WriteLine(_str);
return _str;
}
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:一元运算符表达式
用UnaryExpression做一个5--的表达式:
ConstantExpression _consNum = Expression.Constant(, typeof(int));
UnaryExpression _unaryPlus = Expression.Decrement(_consNum);
Expression<Func<int>> _unaryLam = Expression.Lambda<Func<int>>(_unaryPlus);
Console.WriteLine(_unaryLam.Compile()());
输出结果:
BinaryExpression : 二元运算符表达式
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()(, ));
输出结果:
两个表达式也可以放在一起:(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()(, , ));
输出结果:
三、使用表达式树访问属性
表达式树可以替换反射,但是未必性能就好,要实际测试一下,另外注意Compile调用过程涉及动态代码生成,所以出于性能考虑最好缓存一下生成的表达式树
接下来用Expression Tree的方式完成属性赋值和取值的操作,它们实现在如下两个静态方法中:CreateGetPropertyValueFunc和CreateSetPropertyValueAction。下面是CreateGetPropertyValueFunc的定义,它返回的是一个Func<object.object>委托:
public static Func<object, object> CreateGetPropertyValueFunc()
{
var property = typeof(IFoo).GetProperty("Bar");
var target = Expression.Parameter(typeof(object));
var castTarget = Expression.Convert(target, typeof(IFoo));
var getPropertyValue = Expression.Property(castTarget, property);
var castPropertyvalue = Expression.Convert(getPropertyValue, typeof(object));
return Expression.Lambda<Func<object, object>>(castPropertyvalue , target).Compile();
}
下面是CreateSetPropertyValueAction方法,返回一个Action<object.object>委托:
public static Action<object, object> CreateSetPropertyValueAction()
{
var property = typeof(IFoo).GetProperty("Bar");
var target = Expression.Parameter(typeof(object));
var propertyValue = Expression.Parameter(typeof(object));
var castTarget = Expression.Convert(target, typeof(IFoo));
var castPropertyValue = Expression.Convert(propertyValue, property.PropertyType);
var setPropertyValue = Expression.Call(castTarget, property.GetSetMethod(), castPropertyValue);
return Expression.Lambda<Action<object, object>>(setPropertyValue, target, propertyValue).Compile();
}
注:表达式树的水很深,此处只是入门,以后仍需继续研究使用~
最新文章
- java项目报junit 相关错误
- 32位的Win7系统下安装64位的Sql Sever?
- 常用的user32API说明
- 微软职位内部推荐-Senior Software Development Engineer H/F
- dropdownlist值改变时调用js
- java 中 String 类的几个问题
- 条带深度 队列深度 NCQ IOPS
- div背景等比例缩小
- iOS开发之性能优化
- Week11(11月19日):补课
- 关于JavaScript语法的小笔记
- linux文件特殊属性介绍(s,s,t)
- AutoFac+MVC+WebApi源码----我踩过的坑
- Mybatis经常被问到的面试题
- leetcode55
- 项目发布脚本-nodejs
- String类型转json 转JSONObject 转 JSONArray 以及 遍历
- 单细胞文献分析 Quantitative single-cell rna-seq with unique molecular identifers
- setjmp与longjmp非局部跳转函数的使用
- 生成excel的时候要用双引号。。。。。
热门文章
- Codeforces 713A. Sonya and Queries
- go chapter 7 - 类型
- PMP的六大管理学定律
- Codeforces 500 E. New Year Domino
- [BZOJ4004][JLOI2015]装备购买(贪心+线性基)
- vijos p1777 引水入城(bfs+贪心)
- 【Splay】【启发式合并】hdu6133 Army Formations
- 【DFS】【枚举】Gym - 101246G - Revolutionary Roads
- 【构造】Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) B. High Load
- 【组合数】【乘法逆元】 Codeforces Round #404 (Div. 2) D. Anton and School - 2