对于一个对象,我可以使用反射或者其他方式获取它的所有子类吗?
对于一个对象,我可以使用反射获取它的所有子类吗?
For an object, can I get all its subclasses using reflection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
对于一个对象,我可以使用反射获取它的所有子类吗?
For an object, can I get all its subclasses using reflection?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
您可以加载程序集中的所有类型,然后枚举它们以查看哪些类型实现了对象的类型。您说的是“对象”,因此下面的代码示例不适用于接口。此外,此代码示例仅搜索声明对象的同一程序集。
或者按照注释中的建议,使用此代码示例搜索所有已加载的程序集。
这两个代码示例都要求您添加
using System.Linq;
You can load all types in the Assembly and then enumerate them to see which ones implement the type of your object. You said 'object' so the below code sample is not for interfaces. Also, this code sample only searches the same assembly as the object was declared in.
Or as suggested in the comments, use this code sample to search through all of the loaded assemblies.
Both code samples require you to add
using System.Linq;
获取子类:
并对所有加载的程序集执行此操作
您还可以获取接口:
而要做相反的操作(获取基类),C# 只能有一个基类。
To get subclasses:
And do that for all loaded assemblies
You also can get interfaces:
And to do the opposite (get the base class), C# can only have one base class.