c# - 获取实现类型的具体属性

标签 c# reflection

给定以下类(class)

public Foo 
{
  public Foo() {
    this.Bar = new Bar();
  }

  public IBar Bar{ get; set;}
}

public Bar : IBar
{
 // implemented properties

}

如何使用反射在 Foo 上获取属性 Bar 的具体实现?

instance.GetType().GetProperty("Bar").PropertyType

仅产生接口(interface)。

最佳答案

如果您尝试获取实现 IBar 的类型,您应该获取它的值并采用该类型:

var type = instance.GetType().GetProperty("Bar").GetValue(instance,null).GetType()

关于c# - 获取实现类型的具体属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27016582/

相关文章:

c# - 无法捕获的异常,第 2 部分

c++ - 通过反射在 XML 中的 C/C++ 头文件中定义的嵌套结构/union

c# - 参数 'd' 未绑定(bind)到指定的 LINQ to Entities 查询表达式中

c# - LINQ INNER SELECT WITH TOP

c# - TcpClient 读取 OutOfMemoryException

c# - 从 app.config 配置部分将键值对读入字典

c# - 从对象获取属性信息而不将属性名称作为字符串提供

java - sun.reflect.CallerSensitive 注释是什么意思?

c# - 如何生成制表符分隔的 txt 文件?

c# - 在 Windows CE 设备上部署配置文件以及 exe 和 dll