A bigger expert in .NET can correct me if I am wrong, but I don't believe this is possible. Internally I believe the .NET framework doesn't actually maintain that hierarchy, and it gets flattened when it is compiled to IL code.
For example, the C# code:
class Program : I2
{
static void Main(string[] args)
{
}
}
interface I1
{
}
interface I2 : I1
{
}
After it is built into IL code, it is:
.class private auto ansi beforefieldinit ReflectionTest.Program
extends [mscorlib]System.Object
implements ReflectionTest.I2,
ReflectionTest.I1
{
...
Which is exactly the same as class Program : I1, I2
However, also in the IL is:
.class interface private abstract auto ansi ReflectionTest.I2
implements ReflectionTest.I1
{
...
This means that you could write some logic to get (from my example) I1
and I2
from the Program
class, then query each of those interfaces to see if they implement one of the others...
In other words, since typeof(I2).GetInterfaces()
contains I1
, then you can infer that since typeof(Program).GetInterfaces()
returns I1
and I2
, then Program
might not directly inherit I1
in code.
I emphasize might not because this is also valid C# code and would make the same IL code (and also the same reflection results):
class Program : I1, I2
{
static void Main(string[] args)
{
}
}
interface I1
{
}
interface I2 : I1
{
}
Now Program
both directly and indirectly inherits I1
...