如何使用反射将System.Type传递给泛型方法

时间:2011-09-20 16:21:26

标签: c# entity-framework generics reflection

  

可能重复:
  How to use reflection to call generic Method?

我正在尝试简化一些EF Code First配置。

而不是像这样编写代码:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Asset>().ToTable("Assets");
    modelBuilder.Entity<VideoAsset>().ToTable("VideoAssets");
    modelBuilder.Entity<ImageAsset>().ToTable("ImageAssets");
    ...
}

我将每个类型声明的表包装到一个类中,并使用反射来调用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?)
        }
    }
}

由于编译时安全性,无法将Type添加为通用参数。那么可以使用反射进行相同的调用吗?

目的是打电话给建设者

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    TablePerTypeBuilder<Asset> builder = new TablePerTypeBuilder<Asset>();
    builder.Build(modelBuilder);
}

2 个答案:

答案 0 :(得分:3)

根据建议,您可以使用MakeGenericMethod()。但它有很多丑陋的打字:

var method = modelBuilder.GetType().GetMethod("Entity");
var genericMethod = method.MakeGenericMethod(type);
var entTypConfig = genericMethod.Invoke(modelBuilder, null);
entTypConfig.GetType()
    .InvokeMember("ToTable", BindingFlags.InvokeMethod, null, entTypConfig, 
                  new object[] {type.Name});

答案 1 :(得分:1)

您可以构建表达式并将其编译为委托:

public void Build(DbModelBuilder builder) 
{
  // Stuff

  var param = Expression.Parameter(typeof(DbModelBuilder));
  foreach (var type in types)
  {
    var method = Expression.Call(
      Expression.Constant(this),      // Call to self.
      "BuildInternal",                // The method to call.
      new[] { type },                 // The generic arguments.
      param);                        // The parameters.

    Expression.Lambda(method, param).Compile().DynamicInvoke(builder);
  }
}

执行时可以调用:

public void BuildInternal<T>(DbModelBuilder builder) where T : class
{
  builder.Entity<T>.ToTable(typeof(T).Name);
}