表达树的实际用法

Dmi*_*ruk 66 c# expression-trees

表达树是一个很好的功能,但它的实际用途是什么?它们可以用于某种代码生成或元编程或其他类似的吗?

Mar*_*ell 43

正如Jon所说,我使用它们为.NET 3.5 提供通用运算符.我也使用它们(再次在MiscUtil中)来提供对非默认构造函数的快速访问(你不能使用Delegate.CreateDelegate构造函数,但Expression工作正常).

手动创建表达式树的其他用法:

但实际上,Expression是编写任何动态代码的一种非常通用的方式.远远高于简单的Reflection.Emit,和我的钱,简单比CodeDOM的理解.在.NET 4.0中,您可以获得更多选项.我展示了Expression 在我的博客上编写代码的基础知识.


Jon*_*eet 21

Marc Gravell在MiscUtil中使用它们来实现通用运算符.

  • Darn,打败我吧;-p (13认同)

Mou*_*mit 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();
}
Run Code Online (Sandbox Code Playgroud)

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));
}
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

.......我知道它也可以使用Reflection...但是这个很快或者我可以说相当于Lambda第一次编译后...第一次迭代只是平均10毫秒慢...所以这很Expression Tree神奇.简单而奇妙......我想...... !!!!!!!!


mat*_*uma 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));
Run Code Online (Sandbox Code Playgroud)


Mau*_*fer 8

LINQ提供程序的实现主要通过处理表达式树来完成.我也使用它们从我的代码中删除文字字符串:


Gio*_*rgi 6

我使用表达式树来构建数学表达式求值程序:使用C#中的表达式树构建表达式求值程序


tui*_*oel 4

您可以使用它们为 Google、Flickr 或 Amazon 等网站、您自己的网站或其他数据提供商构建您自己的 linq 提供程序。