c# - 在运行时获取继承的对象类型

标签 c# inheritance reflection interface types

如果你考虑:

class A : IInterface { }

在运行时:

A instance = new A();

instance.GetType(); // returns "A"

IInterface instance = new A();

instance.GetType(); // returns "A"

object instance = new A();
instance.GetType(); // returns "A"

问题:如何获取 IInterface 作为类型?

最佳答案

instance.GetType().GetInterfaces() 将获取实例类型实现或继承的所有接口(interface) (Type.GetInterfaces Method) .

关于c# - 在运行时获取继承的对象类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6439902/

相关文章:

c# - 将盒装对象转换回原始类型

c# - 你知道使用 PRISM 的主要陷阱吗

c# - 从抽象基类返回规范表示子类是否可以接受?

c# - 如何在事先不知道 T 的情况下在运行时优雅地将 IEnumerable<T> 转换为 HashSet<T>

javascript - 我可以用Javascript编写 "Extend"继承函数吗?

java - 多重继承 :Java vs C++

iPhone:如何获取一个类的所有属性?

c# - WCF 的字符串太长

c# - BackgroundAudioPlayer 为 'Playing' 但未调用 GetSampleAsync()

c# - 将字符串从 AJAX 传递到后面的 ASP.NET C# 代码