查找所有派生类型的泛型类

时间:2011-08-12 18:13:47

标签: c# reflection

我有一个泛型类和一个派生类,如下所示。

public class GenericClass<T> { ... }

public class DerivedClass : GenericClass<SomeType> { ... }

如何通过反射找到派生类?我在下面尝试了两种方法,但似乎没有用。

System.Reflection.Assembly.GetExecutingAssembly().GetTypes().Where(t => typeof(GenericClass<>).IsAssignableFrom(t));

System.Reflection.Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(GenericClass<>));

3 个答案:

答案 0 :(得分:38)

var result = System.Reflection.Assembly.GetExecutingAssembly()
    .GetTypes()
    .Where(t => t.BaseType != null && t.BaseType.IsGenericType && 
                t.BaseType.GetGenericTypeDefinition() == typeof(GenericClass<>));

答案 1 :(得分:8)

比这复杂一点。 t.BaseType可以返回null(例如,当t是接口时)。另请注意,Type.IsSubclassOf方法不适用于泛型类型!如果您正在处理泛型类型,则应使用GetTypeDefinition方法。我最近在博客上发表了关于how to get all derived types of a class的文章。这是一个适用于泛型的IsSubclass方法:

public static bool IsSubclassOf(Type type, Type baseType)
{
    if (type == null || baseType == null || type == baseType)
        return false;

    if (baseType.IsGenericType == false)
    {
        if (type.IsGenericType == false)
            return type.IsSubclassOf(baseType);
    }
    else
    {
        baseType = baseType.GetGenericTypeDefinition();
    }

    type = type.BaseType;
    Type objectType = typeof(object);
    while (type != objectType && type != null)
    {
        Type curentType = type.IsGenericType ?
            type.GetGenericTypeDefinition() : type;
        if (curentType == baseType)
            return true;

        type = type.BaseType;
     }

    return false;
}

答案 2 :(得分:1)

因为我需要以递归方式查找所有派生类型,所以我编写了这段代码并与任何可能需要它的人分享:

    public void ListAllDerviedTypes()
    {
        Type entityType = typeof(TableAdapter);
        Assembly assembly = Assembly.LoadFrom(entityType.Assembly.Location);
        Type[] types = assembly.GetTypes();

        List<Type> results = new List<Type>();
        GetAllDerivedTypesRecursively(types, typeof(SiteAndSectorsTable<>), ref results);

        foreach (var type in results)
        {
            Console.WriteLine(type.Name);
        }
    }

    private static void GetAllDerivedTypesRecursively(Type[] types, Type type1, ref List<Type> results)
    {
        if (type1.IsGenericType)
        {
            GetDerivedFromGeneric(types, type1, ref results);
        }
        else
        {
            GetDerivedFromNonGeneric(types, type1, ref results);
        }
    }

    private static void GetDerivedFromGeneric(Type[] types, Type type, ref List<Type> results)
    {
        var derivedTypes = types
            .Where(t => t.BaseType != null && t.BaseType.IsGenericType &&
                        t.BaseType.GetGenericTypeDefinition() == type).ToList();
        results.AddRange(derivedTypes);
        foreach (Type derivedType in derivedTypes)
        {
            GetAllDerivedTypesRecursively(types, derivedType, ref results);
        }
    }


    public static void GetDerivedFromNonGeneric(Type[] types, Type type, ref List<Type> results)
    {
        var derivedTypes = types.Where(t => t != type && type.IsAssignableFrom(t)).ToList();

        results.AddRange(derivedTypes);
        foreach (Type derivedType in derivedTypes)
        {
            GetAllDerivedTypesRecursively(types, derivedType, ref results);
        }
    }