简体   繁体   English

C#表达式树绑定

[英]C# Expression Tree Binding

So what I am trying to do is use expression trees to apply a predicate to each value in a collection (read map or list.All(predicate)). 所以我想做的是使用表达式树将谓词应用于集合中的每个值(读取映射或list.All(predicate))。 It appears that I am not getting the input parameter to the predicate bound to the value supplied by All, and I'm a little stuck. 看来我没有使输入参数绑定到All提供的值的谓词,而且我有些困惑。 Here is the code (using linqpad) that I am working with:: 这是我正在使用的代码(使用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();
}

Thanks and sorry if this was answered in another question (I looked for a while). 谢谢,很抱歉,如果在另一个问题中回答了这个问题(我花了一段时间)。

This does work, but I had to change the Predicate<TObject> to Func<TObject, bool> . 确实可以,但是我不得不将Predicate<TObject>更改为Func<TObject, bool> If you want I can try to change it back. 如果您愿意,我可以尝试将其改回。

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 minutes later... And if you really want to use Predicate<TObject> ... 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM