title: Dynamic casting to a generic interface type
body: <p>I have read through some of the other answers to do a dynamic cast and I am not sure they address the situation I want to solve hence the question.</p>
  
  <p>I have an interface </p>
  
  <pre><code>public interface ICustomTransmitter&lt;T&gt; : IDataTransmitter where T : EventArgs
  {
      event EventHandler&lt;T&gt; DataEvent;
  }
  </code></pre>
  
  <p>and a set of functions that allow me to get the generic type argument at runtime. This is in order to cast to that type and hook up the specific event (this is thought code so please be gentle)</p>
  
  <pre><code>public bool IsTypeOf(Type baseType, Type interfaceType,
      out Type argumenType)
  {
      var interfaces = baseType.GetInterfaces();
      argumenType = null;
  
      foreach (Type @interface in interfaces)
      {
          if (@interface.Name != interfaceType.Name) continue;
          if (@interface.IsGenericType)
          {
              argumenType = @interface.GetGenericArguments()[0];
          }
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p>And the function that uses the magic above </p>
  
  <pre><code>Type argument;
  var generic = typeof (ICustomTransmitter&lt;&gt;);
  if (IsTypeOf(receiver.GetType(),generic ,out argument))
  {
      var created = generic.MakeGenericType(new[] {argument});
  
      //the line of code missing is below
      receiver as created 
  }
  </code></pre>
  
  <p>Is it possible to cast the receiver to that created type? Also I need to have a solution that works in both dot net 3.5 and dot net 4.</p>