Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用反射从泛型返回所有子类化的类,而不给出特定的泛型类型_C#_Generics_Reflection - Fatal编程技术网

C# 如何使用反射从泛型返回所有子类化的类,而不给出特定的泛型类型

C# 如何使用反射从泛型返回所有子类化的类,而不给出特定的泛型类型,c#,generics,reflection,C#,Generics,Reflection,我试图编写一个方法,使用反射返回所有类,这些类是使用泛型的类的子类,而不受泛型类型的限制。例如,在EF中,我想找到所有映射类。这些类的设置如下: public class clientMap : EntityTypeConfiguration<Client> {} public类clientMap:EntityTypeConfiguration{} 我想在程序集中找到属于EntityTypeConfiguration的子类的所有类,而不将Client指定为T。我想返回应用程序中所

我试图编写一个方法,使用反射返回所有类,这些类是使用泛型的类的子类,而不受泛型类型的限制。例如,在EF中,我想找到所有映射类。这些类的设置如下:

public class clientMap : EntityTypeConfiguration<Client> {}
public类clientMap:EntityTypeConfiguration{}
我想在程序集中找到属于
EntityTypeConfiguration
的子类的所有类,而不将
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;
        }
    }
}
静态类类型扩展{
公共静态bool是从OpenGenericType派生的(
这种类型,,
类型openGenericType
) {
Contract.Requires(类型!=null);
Contract.Requires(openGenericType!=null);
Contract.Requires(openGenericType.IsGenericTypeDefinition);
返回类型。GetTypeHierarchy()
.Where(t=>t.IsGenericType)
.Select(t=>t.GetGenericTypeDefinition())
.Any(t=>openGenericType.Equals(t));
}
公共静态IEnumerable GetTypeHierarchy(此类型){
Contract.Requires(类型!=null);
类型currentType=类型;
while(currentType!=null){
收益型;
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<>)));
}
class Foo{}
类栏:Foo{}
类FooBar:Bar{}
[事实]
从OpenGenericFoo()中导出的公共无效区域{
True(typeof(Bar).IsDerivedFromOpenGenericType(typeof(Foo));
}
[事实]
公共void foobaris由OpenGenericFoo()驱动{
True(typeof(FooBar).IsDerivedFromOpenGenericType(typeof(Foo));
}
[事实]
public void string不是从OpenGenericFoo()派生的{
Assert.False(typeof(string).IsDerivedFromOpenGenericType(typeof(Foo));
}

据我所知,您的情况如下

type.BaseType != null &&
type.BaseType.MetadataToken == typeof(EntityTypeConfiguration<>).MetadataToken
type.BaseType!=空的&&
type.BaseType.MetadataToken==typeof(EntityTypeConfiguration).MetadataToken

重复?实际代码中的
openGenericType
应该是什么
EntityTypeConfiguration
还是应该在括号中?对于您的情况,
typeof(EntityTypeConfiguration)
。注意,代码和测试清楚地表明了这一点。好的风格,契约是一种好的方法,应该鼓励使用它(名称空间)。我发现这比控制台应用程序示例的答案更有用。