如何在从特定类型 C# 继承的程序集中查找所有类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1268397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:27:37  来源:igfitidea点击:

How to find all the types in an Assembly that Inherit from a Specific Type C#

c#reflection

提问by aceinthehole

How do you get a collection of all the types that inherit from a specific other type?

您如何获得从特定其他类型继承的所有类型的集合?

采纳答案by Jon Skeet

Something like:

就像是:

public IEnumerable<Type> FindDerivedTypes(Assembly assembly, Type baseType)
{
    return assembly.GetTypes().Where(t => baseType.IsAssignableFrom(t));
}

If you need to handle generics, that gets somewhat trickier (e.g. passing in the open List<>type but expecting to get back a type which derived from List<int>). Otherwise it's simple though :)

如果您需要处理泛型,那会有些棘手(例如,传入开放List<>类型,但希望取回从 派生的类型List<int>)。否则它很简单:)

If you want to exclude the type itself, you can do so easily enough:

如果你想排除类型本身,你可以很容易地做到这一点:

public IEnumerable<Type> FindDerivedTypes(Assembly assembly, Type baseType)
{
    return assembly.GetTypes().Where(t => t != baseType && 
                                          baseType.IsAssignableFrom(t));
}

Note that this will also allow you to specify an interface and find all the types which implement it, rather than just working with classes as Type.IsSubclassOfdoes.

请注意,这也将允许您指定一个接口并找到实现它的所有类型,而不仅仅是像Type.IsSubclassOf这样使用类。

回答by Thomas Danecker

You have to enumerate all types and check for each if it inherits the one you're looking for.

您必须枚举所有类型并检查每个类型是否继承了您要查找的类型。

Some code like the one in this questionmay be useful for you.

一些像这个问题中的代码可能对你有用。

回答by Tim Murphy

The following method will get a collection of types that inherit a type.

以下方法将获取继承类型的类型集合。

C#

C#

public IEnumerable<Type> FindSubClassesOf<TBaseType>()
{   
    var baseType = typeof(TBaseType);
    var assembly = baseType.Assembly;

    return assembly.GetTypes().Where(t => t.IsSubclassOf(baseType));
}

VB.NET

网络

Public Function FindSubClasses(Of TBaseType)() As IEnumerable(Of Type)

    Dim baseType = GetType(TBaseType)
    Dim assembly = baseType.Assembly

    Return From t In assembly.GetTypes() 
           Where t.IsSubClassOf(baseType) 
           Select t

End Function

If you need to include types that implement an interface see @Jon Skeet's answer.

如果您需要包含实现接口的类型,请参阅 @Jon Skeet 的答案。

回答by HappyNomad

Consider using this method (written for a PCL):

考虑使用此方法(为 PCL 编写):

public IEnumerable<Type> FindDerivedTypes( Assembly assembly, Type baseType )
{
        TypeInfo baseTypeInfo = baseType.GetTypeInfo();
        bool isClass = baseTypeInfo.IsClass, isInterface = baseTypeInfo.IsInterface;

        return
            from type in assembly.DefinedTypes
            where isClass ? type.IsSubclassOf( baseType ) :
                  isInterface ? type.ImplementedInterfaces.Contains( baseTypeInfo.AsType() ) : false
            select type.AsType();
}