简体   繁体   中英

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.

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

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.

Reflection and the .NET core are built to make interfaces fast to use. Thus, Type.GetInterfaces returns all the interfaces that the type can respond to, "flattening" the interface hierarchy in the process.

If you want to divine the ancestry of those interfaces, you will need to call GetInterfaces on each interface as well. There is no shortcut because the hierarchy of interfaces is of little value to the CLR runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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