我创建了一个实现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 –