title: Expression tree : avoid DynamicInvoke
body: <p>I use expression trees to build delegates at runtime :</p>
  
  <pre><code>Type type = GetType();
  ParameterExpression parameterType = Expression.Parameter(type);
  ...
  Delegate delegate = Expression.Lambda(*something*, parameterType).Compile();
  </code></pre>
  
  <p>I don't know at compile time the type returned by the method GetType(). Is it possible to invoke my delegate without using the expensive DynamicInvoke() method ?</p>
  
  <p><strong>Edit :</strong></p>
  
  <p>In my application I have a base abstract class :</p>
  
  <pre><code>public abstract class Frame
  {
      public string RawContent { get; set; }
  
      // ...
  }
  </code></pre>
  
  <p>At runtime the application will use some objects which inherits Frame; the type of these objects is not known at compile time, because they will be loaded with MEF (plug-ins). The goal of the application is to filter the objects with erroneous data : for example, if the program must process some objects of a class like this one :</p>
  
  <pre><code>public class Frame01 : Frame
  {
      public int Counter6hours { get; set; }
  
      public int DeltaCounter6hours { get; set; }
  }
  </code></pre>
  
  <p>I would like my users to be able to write, in the configuration file of the application, something like this :</p>
  
  <pre><code>&lt;filtersSection&gt;
      &lt;filters&gt;
              &lt;filter class="Frame01" expression="Counter6hours &lt; 0" /&gt;
              &lt;filter class="Frame01" expression="DeltaCounter6hours &gt; 2500" /&gt;
      &lt;/filters&gt;
  &lt;filtersSection&gt;
  </code></pre>
  
  <p>I manage to create an expression tree and compile it into a delegate for each filter. But I can't cast it into Func because I don't know the type Frame01 at compile time... So, for the moment, I use the method DynamicInvoke() of my delegates, which invokes in a late-bound way the underlying methods. The application will have to handle a large amount of objects, and I fear performance issues... So, in this example, I'm trying to build programmatically a Func object, but I'm not sure it's possible.</p>
  
  <p>PS : Excuse me for my poor English...</p>