c# - 给定一个对象,我如何以编程方式告诉它支持哪些接口(interface)?

标签 c# .net 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
}

我可以用什么替换“The Magic”来显示对象 b 上支持的接口(interface)?

是的,我知道它属于 Base 类,它支持“IBase”,真正的层次结构比这个更复杂。 :)

谢谢! -DF5

编辑:既然我已经看到了答案,我会因为没有通过 Intellisense 被它绊倒而感到愚蠢。 :)

谢谢大家! -DF5

最佳答案

魔法:

foreach (Type iface in b.GetType().GetInterfaces())
    Console.WriteLine(iface.Name);

关于c# - 给定一个对象,我如何以编程方式告诉它支持哪些接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/395549/

相关文章:

c# - 前置条件和后置条件的解释?

c# - 使用 VsConnection WorkItemTrackingHttpClient 补丁通过 VSTS 客户端 API 添加父关系

.net - 如何在 ServiceStack.Text 中使用 EnumMember 属性反序列化可空枚举?

C# 静态类 - 静态类不能有非静态成员

c# - 在 Parallels 8 错误的 Windows 7 上运行 C# Windows 窗体应用程序

c# - .NET:是否有单击并拖动 "Desktop-Like"控件?

.net - 在多个数据库上使用 Entity Framework

java动态类加载

c# - 您如何获得在声明中实际输入的变量名称?

c# - 实例化对象中的所有属性和子属性(C#.net)