如果我错了,.NET中一个更大的专家可以纠正我,但我不相信这是可能的。在内部,我相信.NET框架实际上不会维护这个层次结构,并且在编译为IL代码时它会变得平坦。
例如,C#代码:
class Program : I2
{
static void Main(string[] args)
{
}
}
interface I1
{
}
interface I2 : I1
{
}
它被内置到IL代码后,它是:
.class private auto ansi beforefieldinit ReflectionTest.Program
extends [mscorlib]System.Object
implements ReflectionTest.I2,
ReflectionTest.I1
{
...
哪个是完全相同class Program : I1, I2
然而,在IL中也是:
.class interface private abstract auto ansi ReflectionTest.I2
implements ReflectionTest.I1
{
...
这意味着,你可以写一些逻辑从Program
类获取(从我的例子)I1
和I2
,然后查询每个这些接口的,看看他们是否落实别人的一个... 换句话说由于typeof(I2).GetInterfaces()
包含I1
,那么你就可以推断,由于typeof(Program).GetInterfaces()
回报I1
和I2
,然后Program
可能不直接继承代码I1
。
我强调可能不会因为这也是有效的C#代码,并会作出同样的IL代码(以及相同的反射效果):直接
class Program : I1, I2
{
static void Main(string[] args)
{
}
}
interface I1
{
}
interface I2 : I1
{
}
现在Program
都间接继承I1
..
如果是公共课测试:ITest,ITesting',该怎么办?你为什么要这样? – SLaks
因为我想知道这个类是从哪个接口直接继承的?而不是它从间接继承的接口,动态生成新类型。这有什么关系吗? –
严格地说,你不会*继承*接口,你*实现*它们,所以在这种情况下'Test'类提供了'ITest'和'ITesting'的实现。 “ITest”从“ITesting”继承的事实仅仅意味着实现“ITest”的任何类型都必须提供“ITesting”的实现。 – LukeH