public interface IBar {}
public interface IFoo : IBar {}
typeof(IFoo).BaseType == null
How can I get IBar?
public interface IBar {}
public interface IFoo : IBar {}
typeof(IFoo).BaseType == null
How can I get IBar?
Type[] types = typeof(IFoo).GetInterfaces();
Edit: If you specifically want IBar, you can do:
Type type = typeof(IFoo).GetInterface("IBar");
An interface is not a base type. Interfaces are not part of the inheritance tree.
To get access to interfaces list you can use:
typeof(IFoo).GetInterfaces()
or if you know the interface name:
typeof(IFoo).GetInterface("IBar")
If you are only interested in knowing if a type is implicitly compatible with another type (which I suspect is what you are looking for), use type.IsAssignableFrom(fromType). This is equivalent of 'is' keyword but with runtime types.
Example:
if(foo is IBar) {
// ...
}
Is equivalent to:
if(typeof(IBar).IsAssignableFrom(foo.GetType())) {
// ...
}
But in your case, you are probably more interested in:
if(typeof(IBar).IsAssignableFrom(typeof(IFoo))) {
// ...
}
In addition to what the other posters wrote, you can get the first interface from the GetInterface() list (if the list is not empty) to get the direct parent of IFoo. This would be the exact equivalent of your .BaseType attempt.
"In addition to what the other posters wrote, you can get the first interface from the GetInterface() list (if the list is not empty) to get the direct parent of IFoo. This would be the exact equivalent of your .BaseType attemp"
Um, I'm afraid that's not necessarily the case, especially if the interfaces are COM based.
I've seen cases where the default interface for a COM object was subsequently updated with another interface that specialized the former, but was not first in the array returned by GetInterfaces().
For example, when IFoo is the default interface, and the developer extends IFoo with another interface (rather than changing IFoo and breaking existing applications), which for the sake of example, we'll call "IFoo2" (which specializes IFoo), the IFoo2 interface appears last in the array returned by GetInterfaces().