title: C# Expression Tree Binding
body: <p>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)). 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. Here is the code (using linqpad) that I am working with:: </p>
  
  <pre><code>public class SomeType
  {
    public IEnumerable&lt;bool&gt; Collection { get; set; }
  }
  
  void Main()
  {
    var list = new SomeType {
      Collection = new List&lt;bool&gt; { true, true, true }
    };
    var functor = Compiler((SomeType t) =&gt; t.Collection, (bool x) =&gt; x);
    functor(list).Dump();
  }
  
  MethodInfo FindMethod&lt;TInput&gt;(Type location, string name)
  {
      var handle = location
          .GetMethods(BindingFlags.Static | BindingFlags.Public)
          .Where(method =&gt; method.Name == name).First();
  
      return handle.MakeGenericMethod(typeof(TInput));
  }
  
  Predicate&lt;TObject&gt; Compiler&lt;TObject, TProperty&gt;(
      Expression&lt;Func&lt;TObject, IEnumerable&lt;TProperty&gt;&gt;&gt; selector, 
      Expression&lt;Predicate&lt;TProperty&gt;&gt; predicate)
  {
      var query = FindMethod&lt;TProperty&gt;(typeof(Enumerable), "All");
      var expression = Expression.Call(query,
          new Expression[] {
              Expression.Invoke(selector, selector.Parameters),
              Expression.Lambda&lt;Func&lt;TProperty, bool&gt;&gt;(predicate.Body,
                          Expression.Parameter(typeof(TProperty))),
          });         
  
      return Expression.Lambda&lt;Predicate&lt;TObject&gt;&gt;(expression,
          selector.Parameters).Compile();
  }
  </code></pre>
  
  <p>Thanks and sorry if this was answered in another question (I looked for a while).</p>