我創建了一個實現ICustomTypeDescriptor的泛型類Group。 它只是將泛型類型參數的屬性添加到它自己的屬性中。爲什麼TypeDescriptor.GetProperties在處理ICustomTypeDescriptor時表現出不同的類型和對象
private void InitializeDisplayedProperties()
{
_DisplayedProperties.Add(TypeDescriptor.GetProperties(typeof(Group<T>))["LastItem"]);
_DisplayedProperties.Add(TypeDescriptor.GetProperties(typeof(Group<T>))["GroupId"]);
_DisplayedProperties.Add(TypeDescriptor.GetProperties(typeof(Group<T>))["Count"]);
foreach (PropertyDescriptor myDescr in TypeDescriptor.GetProperties(typeof(T)))
{
_DisplayedProperties.Add(myDescr);
}
}
爲什麼下面的代碼行爲不同?
TypeDescriptor.GetProperties(typeof(Group<IGroupedObject>)).Count //Returns 3 Items of Group only
TypeDescriptor.GetProperties(new Group<IGroupedObject>()).Count //Returns all 31 Items of Group and generic type parameter
我認爲這必須與這些屬性是在對象的實例時間生成的事實有關。但是,不是已使用類型定義的屬性數量?
是否有可能在不實例化類型的情況下解決此問題?
TypeDescriptionProvider似乎是正確的選擇。但是,在嘗試實現它時,由於我的InitializeDisplayedProperties方法(由構造函數調用)中的循環引用產生了未終止的遞歸,因爲我依靠組自己的默認TypeDescriptor來獲取其3個原始PropertyDescriptor。是否有解決方案來訪問原始PropertyDescriptors而不依賴於現在被重寫的TypeDescriptor for Group? – Marwie
@Marwie是的 - 你早期把它們鏈接起來;有關此示例,請參閱HyperDescriptor –