title: How can I load all Entity Framework 4.1 entity configuration entities via reflection?
body: <p>In my <code>OnModelCreating</code> method for my data context I currently am manually mapping all my entity configuration mapping classes manually, like:</p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      modelBuilder.Configurations.Add(new UserMap());
      // 20 or so mapping configuration below
  }
  </code></pre>
  
  <p>I want to streamline this by using reflection, so I have the following code:</p>
  
  <pre><code>    protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          // Find all EntityTypeConfiguration classes in the assembly
          foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
              foreach (Type t in asm.GetTypes())
                  if (t.IsDerivedFromOpenGenericType(typeof(EntityTypeConfiguration&lt;&gt;)))
                      modelBuilder.Configurations.Add(Activator.CreateInstance(t));   
      }
  </code></pre>
  
  <p>the <code>IsDerivedFromOpenGenericType</code> is from <a href="https://stackoverflow.com/questions/6426949/how-can-i-use-reflection-to-return-all-classes-subclassing-from-a-generic-withou">this question</a> and works properly.</p>
  
  <p>The problem is this doesn't compile because <code>Activator.CreateInstance(t)</code> returns an <code>object</code>, but the model builder is expecting a <code>System.Data.Entity.ModelConfiguration.ComplexTypeConfiguration&lt;TComplexType&gt;</code>.  </p>
  
  <p>Normally when using the <code>Activator</code> class I would just cast the object as whatever I expect type <code>t</code> to be (or what I expect the class to take), but since this is using a generics I don't know of a way to do that.</p>
  
  <p>Does anyone have any ideas?</p>