title: How to pass a System.Type into a generic method using reflection
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method">How to use reflection to call generic Method?</a>  </p>
  </blockquote>
  
  
  
  <p>I'm trying to simplify some EF Code First configuration.  </p>
  
  <p>Instead of writing code like this:</p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      modelBuilder.Entity&lt;Asset&gt;().ToTable("Assets");
      modelBuilder.Entity&lt;VideoAsset&gt;().ToTable("VideoAssets");
      modelBuilder.Entity&lt;ImageAsset&gt;().ToTable("ImageAssets");
      ...
  }
  </code></pre>
  
  <p>I'm wrapping the table per type declaration into a class and using reflection to call the modelBuilder</p>
  
  <pre><code>public class TablePerTypeBuilder&lt;TBase&gt; where TBase : class
  {
      public void Build(DbModelBuilder modelBuilder)
      {
          //modelBuilder.Entity&lt;Asset&gt;().ToTable("Assets");
          modelBuilder.Entity&lt;TBase&gt;().ToTable(typeof(TBase).Name);
  
          //modelBuilder.Entity&lt;VideoAsset&gt;().ToTable("VideoAssets");
          //modelBuilder.Entity&lt;ImageAsset&gt;().ToTable("ImageAssets");
          var types = from a in AppDomain.CurrentDomain.GetAssemblies()
                      from t in a.GetTypes()
                      where typeof(TBase).IsAssignableFrom(t)
                      select t;
  
          foreach (Type type in types)
          {           
              modelBuilder.Entity&lt;type&gt;().ToTable(type.Name); 
              //Error - The type or namespace name 'type' could not be found (are you missing a using directive or an assembly reference?)
          }
      }
  }
  </code></pre>
  
  <p>It's not possible to add a Type as a generic parameter due to compile time safety.  So is it possible to make same call using refection?</p>
  
  <p>The aim is to call the builder like</p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      TablePerTypeBuilder&lt;Asset&gt; builder = new TablePerTypeBuilder&lt;Asset&gt;();
      builder.Build(modelBuilder);
  }
  </code></pre>