1
我有一些非常複雜的對象傳遞給我的回調,我不控制對象設計,並不真正知道那裏會發生什麼。我所知道的是,它可能實現了SpecificInterface
接口,該接口具有公共獲取公共屬性SpecificProperty
。爲什麼GetProperty()沒有找到大多數派生類型的屬性,但會發現它是類型實現的接口之一?
我有這樣的代碼:
dynamic theObjectPassed = ...;
Type objectType = theObjectPassed.GetType();
var objectProperty = objectType.GetProperty("SpecificProperty");
Type interfaceType = objectType.GetInterface("SpecificInterface");
var interfaceProperty = interfaceType.GetProperty("SpecificProperty");
,不知何故objectProperty
成爲null
但interfaceProperty
變爲非空。
爲什麼在接口中找到該屬性,但在該接口派生的類中找不到該屬性?
[顯式接口實現](http://msdn.microsoft.com/zh-cn/library/ms173157.aspx),也許? –
@Damien_The_Unbeliever:哎喲...我怎麼看不到它? – sharptooth
另一種可能性可能是該屬性是在基類上實現的,並且派生類定義了具有相同名稱的非屬性(隱藏基類屬性)。 (編輯:這是錯誤的,測試顯示'Type.GetProperty'仍然會找到基類屬性。) – hvd