Reputation: 1420
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.
Upvotes: 4
Views: 2016
Reputation: 36082
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.
Upvotes: 3