我正在尝试使用以下代码获取IEntityModelBuilder的所有实现,但它返回一个空集合。
public class EntityFrameworkDbContext : DbContext
{
//constructor(s) and entities DbSets...
private static IEnumerable<IEntityModelBuilder<IEntity>> _entitymodelBuilders;
internal IEnumerable<IEntityModelBuilder<IEntity>> EntityModelBuilders
{
get
{
if (_entitymodelBuilders == null)
{
var type = typeof(IEntityModelBuilder<IEntity>);
_entitymodelBuilders = Assembly.GetAssembly(type).GetTypes()
.Where(t => type.IsAssignableFrom(t) && t.IsClass)
.Select(t => (IEntityModelBuilder<IEntity>)Activator.CreateInstance(t, new object[0]));
}
return _entitymodelBuilders;
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
foreach (var builder in EntityModelBuilders)
builder.Build(modelBuilder);
base.OnModelCreating(modelBuilder);
}
}
internal interface IEntityModelBuilder<TEntity> where TEntity : IEntity
{
void Build(DbModelBuilder modelBuilder);
}
//sample implementation
internal class UserModelBuilder : IEntityModelBuilder<User>
{
public void Build(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.ToTable("users")
.HasKey(e => e.Id);
modelBuilder.Entity<User>()
.Property(e => e.Id)
.HasColumnName("id");
modelBuilder.Entity<User>()
.Property(e => e.Email)
.HasColumnName("email");
//and so on...
}
}
如果我用
更改类型var type = typeof(IEntityModelBuilder<User>);
获取代码的类型运行正常并返回预期的UserModelBuilder。我怎么能用泛型做到这一点?
答案 0 :(得分:4)
尽管Slava的解决方案有效,但由于Contains
,它通常不是完全安全的。 可能是某些其他接口/类型可能包含您要搜索的接口的名称。在这种情况下,假设您有另一个名为IEntityModelBuilderHelper
的接口。
此外,只需很少的努力,您就可以将此代码概括为更强大的功能。请考虑以下两种方法:
public static IEnumerable<Type> GetAllTypes(Type genericType)
{
if (!genericType.IsGenericTypeDefinition)
throw new ArgumentException("Specified type must be a generic type definition.", nameof(genericType));
return Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => t.GetInterfaces()
.Any(i => i.IsGenericType &&
i.GetGenericTypeDefinition().Equals(genericType)));
}
和
public static IEnumerable<Type> GetAllTypes(Type genericType, params Type[] genericParameterTypes)
{
if (!genericType.IsGenericTypeDefinition)
throw new ArgumentException("Specified type must be a generic type definition.", nameof(genericType));
return Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => t.GetInterfaces()
.Any(i => i.IsGenericType &&
i.GetGenericTypeDefinition().Equals(genericType) &&
i.GetGenericArguments().Count() == genericParameterTypes.Length &&
i.GetGenericArguments().Zip(genericParameterTypes,
(f, s) => s.IsAssignableFrom(f))
.All(z => z)));
}
前者将为您提供实现所提供的泛型类型定义的所有类型,即typeof(MyGenericType<>)
,对泛型类型参数没有任何约束。后者将使用提供的类型约束执行相同的操作。
考虑以下类型:
public interface IFoo<T> { }
public interface IEntity { }
public class A : IEntity { }
public class Foo : IFoo<IEntity> { }
public class FooA : IFoo<A> { }
public class FooS : IFoo<string> { }
var types = GetAllTypes(typeof(IFoo<>));
将返回3种类型:{ Foo, FooA, FooS }
而var types = GetAllTypes(typeof(IFoo<>), typeof(IEntity));
只返回两种类型:{ Foo, FooA }
。
答案 1 :(得分:2)
您可以尝试example。
<强>声明:强>
public interface IEntity { }
public class Entity1 : IEntity { }
public class Entity2 : IEntity { }
public interface IEntityModelBuilder<out T> where T : IEntity { }
public class BaseClass1 : IEntityModelBuilder<Entity1>
{
public BaseClass1(int a) { }
}
public class BaseClass2 : IEntityModelBuilder<Entity2>
{
public BaseClass2(int a) { }
}
<强>用法:强>
List<IEntityModelBuilder<IEntity>> objects = Assembly.GetExecutingAssembly().GetTypes()
.Where(x => x.GetInterfaces().Any(y => y.IsGenericType && && y.Name == "IEntityModelBuilder`1"))
.Select(x => (IEntityModelBuilder<IEntity>)Activator.CreateInstance(x, new object[] { 0 })).ToList();