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

↘锁芯ラ 提交于 2020-01-01 04:30:06

问题


    class Foo { }

    class Foo1 : Foo { }

    class Foo2 : Foo { }

How would I be able to get all the classes that use Foo as a base class? The inherited classes aren't necessary in the same assembly.


回答1:


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)));


来源:https://stackoverflow.com/questions/1665120/how-can-i-get-all-the-inherited-classes-of-a-base-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!