title: Dynamic cast to generic type
body: <p>Just a quickie before the weekend rolls in...</p>
  
  <p>I have a Method with the following signature, that I need to invoke:</p>
  
  <pre><code>public interface IObjectProvider&lt;T&gt;
  {
      T Get(Predicate&lt;T&gt; condition);
  }
  </code></pre>
  
  <p>This will provide me with a <code>T</code> from whatever kind of source, that meets the predicate criteria.</p>
  
  <p>Now, this has to be called from a context where all I have is the following:</p>
  
  <pre><code>//the actual predicate that's going to be evaluated
  var predicate = predicateProperty.GetValue(invocation.InvocationTarget, null);
  
  //The type that should go into the call as type param
  Type relationTargetType = relationDefinition.RelatedType;
  </code></pre>
  
  <p>As you might guess, the compiler won't let me use the <code>predicate</code> variable as parameter. What I need to do is convert this object into a Predicate, but Generic type params must be compile-time-constant, so this won't work.</p>
  
  <p>I've started messing around with this, but no success so far:</p>
  
  <pre><code>Type genericPredicateType = typeof(Predicate&lt;&gt;);
  Type specificPredicateType= genericPredicateType.MakeGenericType(relationTargetType);
  Convert.ChangeType(predicate, specificPredicateType)
  </code></pre>
  
  <p>How on earth can I mash this up?</p>
  
  <p><strong>EDIT:</strong> I thought this was a rather use-case-agnostic question, but obviously I was wrong. So, since there is such a fuss as to what I do, what I have and why and whatnot, here's a lot more background info. I am trying to resolve relations between objects in a Proxy (Castle Dynamic Proxy). The following Snippet should explain the kind of relation I want to depict:</p>
  
  <pre><code>public class Order
  {
      public virtual int Id { get; set; } // OR-Mapped
      public virtual DateTime OrderDate { get; set; } // OR-Mapped
  
      [RelatedObject(typeof(Address), "DeliveryAddressPredicate")]
      public virtual Address DeliveryAddress { get; set; }
  
      public Predicate&lt;Address&gt; DeliveryAddressPredicate
      {
          get { return new Predicate&lt;Address&gt;(a =&gt; OrderDate &gt;= a.ValidFrom &amp;&amp; OrderDate &lt;= a.ValidTo); }
      }
  }
  
  public class Address
  {
      public virtual DateTime ValidFrom { get; set; } // OR-Mapped
      public virtual DateTime ValidTo { get; set; } // OR-Mapped
  
      //Not OR-Mapped
      [RelatedList(typeof(Order), "OrdersPredicate")]
      public virtual IList&lt;Order&gt; Orders { get; set; }
  
      public Predicate&lt;Order&gt; OrdersPredicate
      {
          get { return new Predicate&lt;Order&gt;(o =&gt; o.OrderDate &gt;= ValidFrom &amp;&amp; o.OrderDate &lt;= ValidTo); }
      }
  </code></pre>
  
  <p>To sum it up, this is supposed to become a Fuzzy OR-Mapping, meant to extend NHibernate in a project or two.</p>
  
  <p>How did I mean to get this to work? The address is proxied, and when a call to a property with one of my CustomAttributes is made, i use DynamicProxy's IInterceptor interface to resolve the relation. The main problem is that this resolving has to happen in the IInterceptor.Intercept() Method which has only one Param (<a href="http://www.codeproject.com/Articles/9055/Castle-s-DynamicProxy-for-NET" rel="noreferrer">see here</a>), and I have no generic type param available. So, in the end it all boils down to a simple .Net question again: I have a <code>Type</code> stored in a variable and a <code>Method</code> that has to be called with a parameter generic of the aforesaid type...</p>
  
  <p>Sorry for any mistakes made (like calling <code>var</code> a <code>Type</code> - man that was a rough one), it's been quite a day ;-)</p>