给定以下类(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/