开发者

How can I get all the inherited classes of a base class? [duplicate]

开发者 https://www.devze.com 2022-12-09 22:20 出处:网络
This question already has answers here: How to find all the types in an Assembly that Inherit from a Specific Type C#
This question already has answers here: How to find all the types in an Assembly that Inherit from a Specific Type C# (4 answers) Get all derived types of a type (8 answers) Closed 9 years ago.
    class Foo { }

    class Foo1 : Foo { }

    class Foo2 : Foo { }

How would I be able to get all the开发者_如何学JAVA classes that use Foo as a base class? The inherited classes aren't necessary in the same assembly.


This is not fast, but as long as Foo is a concrete type (not an interface), then it should work. Foo itself is not returned by this code.

AppDomain.CurrentDomain.GetAssemblies()
                       .SelectMany(assembly => assembly.GetTypes())
                       .Where(type => type.IsSubclassOf(typeof(Foo)));
0

精彩评论

暂无评论...
验证码 换一张
取 消