Possible Duplicate:
How to use reflection to call generic Method?
I'm trying to simplify some EF Code First configuration.
Instead of writing code like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Asset>().ToTable("Assets");
modelBuilder.Entity<VideoAsset>().ToTable("VideoAssets");
modelBuilder.Entity<ImageAsset>().ToTable("ImageAssets");
...
}
I'm wrapping the table per type declaration into a class and using reflection to call the modelBuilder
public class TablePerTypeBuilder<TBase> where TBase : class
{
public void Build(DbModelBuilder modelBuilder)
{
//modelBuilder.Entity<Asset>().ToTable("Assets");
modelBuilder.Entity<TBase>().ToTable(typeof(TBase).Name);
//modelBuilder.Entity<VideoAsset>().ToTable("VideoAssets");
//modelBuilder.Entity<ImageAsset>().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<type>().ToTable(type.Name);
//Error - The type or namespace name 'type' could not be found (are you missing a using directive or an assembly reference?)
}
}
}
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?
The aim is to call the builder like
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
TablePerTypeBuilder<Asset> builder = new TablePerTypeBuilder<Asset>();
builder.Build(modelBuilder);
}