http://www.cnblogs.com/ldp615/archive/2011/09/15/expression-extension-methods.html
.net 中创建 Expression Trees 最简单的方式是使用 lambda
表达式:
1
2
|
Expression<Func<Person, bool>> exp =
p => p.Name.Contains("ldp") && p.Birthday.Value.Year > 1990;
|
其中 Person 类定义如下:
1
2
3
4
|
public class Person {
public string Name { get; set; }
public DateTime? Birthday { get; set; }
}
|
但有些时候,要动态创建 Expression Trees,我们要用到 System.Linq.Expressions 命名空间中的 Expression
类。
使用 Expression 类 中的静态方法,前面的 Expression
Trees 可如下创建:
1
2
3
4
5
6
7
8
9
10
|
var parameter = Expression.Parameter(typeof(Person), "p");
var left = Expression.Call(
Expression.Property(parameter, "Name"),
typeof(string).GetMethod("Contains"),
Expression.Constant("ldp"));
var right = Expression.GreaterThan(
Expression.Property(Expression.Property(Expression.Property(parameter, "Birthday"), "Value"), "Year"),
Expression.Constant(1990));
var body = Expression.AndAlso(left, right);
var lambda = Expression.Lambda<Func<Person, bool>>(body, parameter);
|
你应该注意到第 7 行高亮部分,三个重复的 Expression.Property,显得非常臃肿,导致可读性也很差。
可以用下面几个扩展方法予以简化:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public static class ExpressionExtensions {
public static Expression AndAlso(this Expression left, Expression right) {
return Expression.AndAlso(left, right);
}
public static Expression Call(this Expression instance, string methodName, params Expression[] arguments) {
return Expression.Call(instance, instance.Type.GetMethod(methodName), arguments);
}
public static Expression Property(this Expression expression, string propertyName) {
return Expression.Property(expression, propertyName);
}
public static Expression GreaterThan(this Expression left, Expression right) {
return Expression.GreaterThan(left, right);
}
public static Expression<TDelegate> ToLambda<TDelegate>(this Expression body, params ParameterExpression[] parameters) {
return Expression.Lambda<TDelegate>(body, parameters);
}
}
|
这五个扩展方法相当简单,没什么技术含量。可以根据自己需要,添加更多的扩展方法。
前面的代码简化成:
1
2
3
4
|
var parameter = Expression.Parameter(typeof(Person), "p");
var left = parameter.Property("Name").Call("Contains", Expression.Constant("ldp"));
var right = parameter.Property("Birthday").Property("Value").Property("Year").GreaterThan(Expression.Constant(1990));
var lambda = left.AndAlso(right).ToLambda<Func<Person, bool>>(parameter);
|
是不是好多了。
简单编码,快乐生活!
最新文章
- java的4种代码块
- Ceph剖析:Paxos算法实现
- hdu 2334 March of the Penguins
- Request.QueryString 不能像使用方法那样使用不可调用
- hdu1695(莫比乌斯)或欧拉函数+容斥
- Codeforces758C
- 运维技巧-Nginx日志格式
- Node.js实战项目学习系列(2) 开发环境和调试工具
- IdentityServer4(9)- 使用OpenID Connect添加用户身份验证(implicit)
- javascript常用的操作
- mybatis进阶-5resultMap总结
- 【摄像头】Global Shutter(全局快门)与Rolling Shutter(卷帘快门)的区别与比较
- Hive 系列(二)权限管理
- linux上jenkins连接windows并执行exe文件
- myBatsi调用存储过程
- 使用java中replaceAll方法替换字符串中的反斜杠
- iOS取整
- FullCalendar Timeline View 使用
- Nuget Tips
- 【uoj#51】[UR #4]元旦三侠的游戏 博弈论+dp
热门文章
- 树莓派应用Docker
- Visio操作【未完】
- vue3.0安装
- 结合场景使用Redis缓存与数据库同步
- NOIP 模拟 $20\; \rm z$
- 带你读AI论文丨LaneNet基于实体分割的端到端车道线检测
- SQL中的聚合函数运用
- 【java虚拟机】内存溢出解决思路
- 【转】java内存溢出的场景及解决办法
- 【转】Linux命令:ps -ef |grep java