title: Casting generic type instances created using Reflection
body: <p>I'm creating instances of a generic type using reflection:</p>
  
  <pre><code>public interface IModelBuilder&lt;TModel&gt;
  {
      TModel BuildModel();
  }
  
  public class MyModel
  {
      public string Name { get; set; }
  }
  
  public class MyModelBuilder : IModelBuilder&lt;MyModel&gt;
  {
      public MyModel BuildModel()
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>At runtime all we know is the Type of model e.g. <code>MyModel</code>. I can find instances of the relevant model builder like so:</p>
  
  <pre><code>var modelBuilders = from t in Assembly.GetExecutingAssembly().GetTypes()
                  from i in t.GetInterfaces()
                  where i.IsGenericType
                          &amp;&amp; i.GetGenericTypeDefinition() == typeof(IModelBuilder&lt;&gt;)
                          &amp;&amp; i.GetGenericArguments()[0] == modelType
                  select t;
  
  var builder = Activator.CreateInstance(modelBuilders.First());
  </code></pre>
  
  <p>But I'm not sure how I can then cast the instance as <code>IModelBuilder&lt;TModel&gt;</code> so I can call and work with the result of <code>BuildModel()</code>.</p>