表达式树是一个很好的功能,但它的实际用途是什么?它们可以用于某种代码生成或元编程或其他类似的吗?
答案 0 :(得分:41)
正如Jon所说,我使用它们为{3.5}提供了generic operators。我也使用它们(再次在MiscUtil中)来提供对非默认构造函数的快速访问(你不能将Delegate.CreateDelegate
与构造函数一起使用,但Expression
工作正常)。
手动创建表达式树的其他用法:
但实际上,Expression是编写任何动态代码的一种非常通用的方式。比Reflection.Emit
简单得多,而且对于我的钱,比CodeDOM更容易理解。在.NET 4.0中,您可以使用even more options。我展示了通过Expression
on my blog编写代码的基础知识。
答案 1 :(得分:20)
Marc Gravell在MiscUtil中使用它们来实现generic operators。
答案 2 :(得分:15)
我刚刚使用generic filter function
创建了ExpressionTree
..我想和你们一起share
......
Start
var allFiltered= Filter(AllCustomer, "Name", "Moumit");
public static List<T> Filter<T>(this List<T> Filterable, string PropertyName, object ParameterValue)
{
ConstantExpression c = Expression.Constant(ParameterValue);
ParameterExpression p = Expression.Parameter(typeof(T), "xx");
MemberExpression m = Expression.PropertyOrField(p, PropertyName);
var Lambda = Expression.Lambda<Func<T, Boolean>>(Expression.Equal(c, m), new[] { p });
Func<T, Boolean> func = Lambda.Compile();
return Filterable.Where(func).ToList();
}
One More
string singlePropertyName=GetPropertyName((Property.Customer p) => p.Name);
public static string GetPropertyName<T, U>(Expression<Func<T, U>> expression)
{
MemberExpression body = expression.Body as MemberExpression;
// if expression is not a member expression
if (body == null)
{
UnaryExpression ubody = (UnaryExpression)expression.Body;
body = ubody.Operand as MemberExpression;
}
return string.Join(".", body.ToString().Split('.').Skip(1));
}
Make it more expandable
string multiCommaSeparatedPropertyNames=GetMultiplePropertyName<Property.Customer>(c => c.CustomerId, c => c.AuthorizationStatus)
public static string GetMultiplePropertyName<T>(params Expression<Func<T, object>>[] expressions)
{
string[] propertyNames = new string[expressions.Count()];
for (int i = 0; i < propertyNames.Length; i++)
{
propertyNames[i] = GetPropertyName(expressions[i]);
}
return propertyNames.Join();
}
.......我知道它也可以使用Reflection
来完成...但是这个很快或者我可以说在第一次编译后等同于Lambda
...第一次迭代只是平均10毫秒的缓慢......所以这是Expression Tree
魔法。简单而美妙......我想...... !!!!!!!!
答案 3 :(得分:12)
我使用它们来创建动态查询,无论是用于排序还是过滤数据。举个例子:
IQueryable<Data.Task> query = ctx.DataContext.Tasks;
if (criteria.ProjectId != Guid.Empty)
query = query.Where(row => row.ProjectId == criteria.ProjectId);
if (criteria.Status != TaskStatus.NotSet)
query = query.Where(row => row.Status == (int)criteria.Status);
if (criteria.DueDate.DateFrom != DateTime.MinValue)
query = query.Where(row => row.DueDate >= criteria.DueDate.DateFrom);
if (criteria.DueDate.DateTo != DateTime.MaxValue)
query = query.Where(row => row.DueDate <= criteria.DueDate.DateTo);
if (criteria.OpenDate.DateFrom != DateTime.MinValue)
query = query.Where(row => row.OpenDate >= criteria.OpenDate.DateFrom);
var data = query.Select(row => TaskInfo.FetchTaskInfo(row));
答案 4 :(得分:8)
LINQ提供程序的实现主要通过处理表达式树来完成。我也使用它们从我的代码中删除文字字符串:
答案 5 :(得分:6)
我使用表达式树来构建数学表达式求值程序:Building Expression Evaluator with Expression Trees in C#
答案 6 :(得分:4)
您可以使用它们为Google或Flickr或亚马逊等网站,您自己的网站或其他数据提供商构建自己的linq提供商。
答案 7 :(得分:2)
最初由Jomo Fisher,Gustavo Guerra发布了修订版 static string dictionary。
通过表达式树,一个动态表达式,提供一个真正的(读取:荒谬)字典。
该实现创建了一个动态决策树,根据输入字符串的长度,然后是第一个字母,然后是第二个字母等来选择corrent值。
这最终比同等的词典运行得快得多。