给定一个Object,我如何以编程方式告诉它支持哪些接口?

DrF*_*yd5 2 .net c# reflection interface

鉴于这种:

Interface IBase {string X {get;set;}}
Interface ISuper {string Y {get;set;}}

class Base : IBase {etc...}
class Super : Base, ISuper {etc...}

void Questionable (Base b) {
  Console.WriteLine ("The class supports the following interfaces... ")
  // The Magic Happens Here
}
Run Code Online (Sandbox Code Playgroud)

有什么可以替换"魔术"来显示对象b上支持的接口?

是的,我知道作为类Base它支持"IBase",真正的层次结构更加复杂.:)

谢谢!-DF5

编辑:现在我已经看到了答案,我感到愚蠢的是没有通过Intellisense绊倒它.:)

谢谢大家!-DF5

Ily*_*kov 8

b.GetType().GetInterfaces()


Dia*_*tis 8

魔法 :

foreach (Type iface in b.GetType().GetInterfaces())
    Console.WriteLine(iface.Name);
Run Code Online (Sandbox Code Playgroud)