我怎样才能得到一个基类的所有继承类的? [英] How can I get all the inherited classes of a base class?

查看:387
本文介绍了我怎样才能得到一个基类的所有继承类的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆