我正在尝试编写一个使用反射的方法,以返回作为使用泛型的类的子类的所有类,而不受泛型类型的限制。例如,在EF中,我想找到所有的映射类。类的设置如下:
public class clientMap : EntityTypeConfiguration<Client> {}
我想在我的程序集中查找属于EntityTypeConfiguration<T>
的子类的所有类,而不将Client
指定为T。我想在不进行硬编码的情况下返回应用程序中所有类的实体类型配置
如果没有泛型,我会遍历程序集中的类型,检查是否为type.IsSubclassOf(typeof(BaseClass))
,但我不确定在处理泛型时如何做到这一点。
我相信你想要这样的东西:
static class TypeExtensions {
public static bool IsDerivedFromOpenGenericType(
this Type type,
Type openGenericType
) {
Contract.Requires(type != null);
Contract.Requires(openGenericType != null);
Contract.Requires(openGenericType.IsGenericTypeDefinition);
return type.GetTypeHierarchy()
.Where(t => t.IsGenericType)
.Select(t => t.GetGenericTypeDefinition())
.Any(t => openGenericType.Equals(t));
}
public static IEnumerable<Type> GetTypeHierarchy(this Type type) {
Contract.Requires(type != null);
Type currentType = type;
while (currentType != null) {
yield return currentType;
currentType = currentType.BaseType;
}
}
}
这些测试通过:
class Foo<T> { }
class Bar : Foo<int> { }
class FooBar : Bar { }
[Fact]
public void BarIsDerivedFromOpenGenericFoo() {
Assert.True(typeof(Bar).IsDerivedFromOpenGenericType(typeof(Foo<>)));
}
[Fact]
public void FooBarIsDerivedFromOpenGenericFoo() {
Assert.True(typeof(FooBar).IsDerivedFromOpenGenericType(typeof(Foo<>)));
}
[Fact]
public void StringIsNotDerivedFromOpenGenericFoo() {
Assert.False(typeof(string).IsDerivedFromOpenGenericType(typeof(Foo<>)));
}
据我所知,以下内容应该足够
type.BaseType != null &&
type.BaseType.MetadataToken == typeof(EntityTypeConfiguration<>).MetadataToken