C# 为什么GetProperty()找不到最派生的类型上的属性,而是找到该类型实现的接口之一?
我将一些非常复杂的对象传递到回调中,我无法控制对象设计,也不知道在那里会发生什么。我所知道的是,它可能实现了一个C# 为什么GetProperty()找不到最派生的类型上的属性,而是找到该类型实现的接口之一?,c#,.net,reflection,C#,.net,Reflection,我将一些非常复杂的对象传递到回调中,我无法控制对象设计,也不知道在那里会发生什么。我所知道的是,它可能实现了一个SpecificInterface接口,该接口具有SpecificPropertypublic属性的公共getter 我有以下代码: dynamic theObjectPassed = ...; Type objectType = theObjectPassed.GetType(); var objectProperty = objectType.GetProperty("Specif
SpecificInterface
接口,该接口具有SpecificProperty
public属性的公共getter
我有以下代码:
dynamic theObjectPassed = ...;
Type objectType = theObjectPassed.GetType();
var objectProperty = objectType.GetProperty("SpecificProperty");
Type interfaceType = objectType.GetInterface("SpecificInterface");
var interfaceProperty = interfaceType.GetProperty("SpecificProperty");
不知何故,objectProperty
变为null
,但interfaceProperty
变为非null
为什么在接口中找到属性,但在从该接口派生的类中找不到属性?创建复杂对象的类必须通过显式接口实现扩展接口
SpecificInterface
;所以,除非类的对象是通过显式接口访问的,否则它的属性是不可访问的。看,也许吧?@Damien\u异教徒:哎哟。。。另一种可能是属性是在基类上实现的,派生类定义了一个同名的非属性(隐藏基类属性)。(编辑:这是错误的,测试表明Type.GetProperty
仍然可以找到基类属性。)