简体   繁体   English

如何找到类型的接口继承层次结构?

[英]How do I find the Interface Inheritance Hierarchy For a type?

My purpose is to find out if a class implements an interface directly. 我的目的是找出一个类是否直接实现了一个接口。 In the example below, class B implements interface IB and interface IB implements IA. 在下面的示例中,B类实现接口IB,接口IB实现IA。

How do I find out the inheritance hierarchy? 我如何找出继承层次结构? When we view a type in Object Browser, it shows a detailed hierarchy. 当我们在对象浏览器中查看类型时,它会显示详细的层次结构。 How can I achieve similar result? 我怎样才能达到类似的效果?

interface IA
{
    string Member1 { get;set; }
}

interface IB : IA
{
    string Member2 { get; set; }
}

class B : IB
{
    public string Member1 { get; set; }
    public string Member2 { get; set; }
}

Reflector Screenshot 反射器截图

alt text http://img571.imageshack.us/img571/7485/49211426.png 替代文字http://img571.imageshack.us/img571/7485/49211426.png

In the screenshot taken from reflector it shows the hierarchy of the interfaces as well. 在从反射器获取的屏幕截图中,它还显示了接口的层次结构。

How can I find out the interface hierarchy for a Type. 如何找到Type的接口层次结构。

Reflection and the .NET core are built to make interfaces fast to use. 构建了Reflection和.NET内核,使接口可以快速使用。 Thus, Type.GetInterfaces returns all the interfaces that the type can respond to, "flattening" the interface hierarchy in the process. 因此,Type.GetInterfaces返回该类型可以响应的所有接口,“展平”流程中的接口层次结构。

If you want to divine the ancestry of those interfaces, you will need to call GetInterfaces on each interface as well. 如果要分配这些接口的祖先,则还需要在每个接口上调用GetInterfaces。 There is no shortcut because the hierarchy of interfaces is of little value to the CLR runtime. 没有捷径,因为接口的层次结构对CLR运行时没什么价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM