我怎样才能得到一个基类的所有继承类的? [英] How can I get all the inherited classes of a base class?
本文介绍了我怎样才能得到一个基类的所有继承类的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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.
推荐答案
这是不是很快,但只要富
是一个具体类型(而不是接口),那么它应该工作。 富
本身不受此代码返回。
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)));
这篇关于我怎样才能得到一个基类的所有继承类的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文