Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么GetProperty()找不到最派生的类型上的属性,而是找到该类型实现的接口之一?_C#_.net_Reflection - Fatal编程技术网

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
仍然可以找到基类属性。)