C#表达式树绑定

时间:2011-10-13 19:21:50

标签: c# linq expression-trees

所以我要做的是使用表达式树将谓词应用于集合中的每个值(读取map或list.All(谓词))。看来我没有得到绑定到All提供的值的谓词的输入参数,我有点卡住了。这是我正在使用的代码(使用linqpad)::

public class SomeType
{
  public IEnumerable<bool> Collection { get; set; }
}

void Main()
{
  var list = new SomeType {
    Collection = new List<bool> { true, true, true }
  };
  var functor = Compiler((SomeType t) => t.Collection, (bool x) => x);
  functor(list).Dump();
}

MethodInfo FindMethod<TInput>(Type location, string name)
{
    var handle = location
        .GetMethods(BindingFlags.Static | BindingFlags.Public)
        .Where(method => method.Name == name).First();

    return handle.MakeGenericMethod(typeof(TInput));
}

Predicate<TObject> Compiler<TObject, TProperty>(
    Expression<Func<TObject, IEnumerable<TProperty>>> selector, 
    Expression<Predicate<TProperty>> predicate)
{
    var query = FindMethod<TProperty>(typeof(Enumerable), "All");
    var expression = Expression.Call(query,
        new Expression[] {
            Expression.Invoke(selector, selector.Parameters),
            Expression.Lambda<Func<TProperty, bool>>(predicate.Body,
                        Expression.Parameter(typeof(TProperty))),
        });         

    return Expression.Lambda<Predicate<TObject>>(expression,
        selector.Parameters).Compile();
}

感谢和抱歉,如果在另一个问题中回答了这个问题(我看了一会儿)。

1 个答案:

答案 0 :(得分:1)

这确实有效,但我必须将Predicate<TObject>更改为Func<TObject, bool>。如果你愿意我可以尝试改回来。

static Predicate<TObject> Compiler<TObject, TProperty>(
    Expression<Func<TObject, IEnumerable<TProperty>>> selector,
    Expression<Func<TProperty, bool>> predicate)
{
    var query = FindMethod<TProperty>(typeof(Enumerable), "All");
    var expression = Expression.Call(
        query,
        Expression.Invoke(selector, selector.Parameters), 
        predicate);

    return Expression
        .Lambda<Predicate<TObject>>(expression, selector.Parameters)
        .Compile();
}

5分钟后......如果你真的想使用Predicate<TObject> ......

static Predicate<TObject> Compiler<TObject, TProperty>(
    Expression<Func<TObject, IEnumerable<TProperty>>> selector,
    Expression<Predicate<TProperty>> predicate)
{
    var query = FindMethod<TProperty>(typeof(Enumerable), "All");

    var predicateAsFunc = Expression.Lambda<Func<TProperty, bool>>(
        predicate.Body, 
        predicate.Parameters);

    var expression = Expression.Call(
        query,
        Expression.Invoke(selector, selector.Parameters), 
        predicateAsFunc);

    return Expression
        .Lambda<Predicate<TObject>>(expression, selector.Parameters)
        .Compile();
}