获取类型的默认 PropertyDescriptor
我通过实现 ICustomTypeDescriptor
自定义对象类型在 PropertyGrid
中的显示方式。 我允许用户创建自己的自定义属性,这些属性存储在单个键和值字典中。 我可以为这些值创建所有 PropertyDescriptors
并在属性网格中查看它们。 不过,我还想显示所有默认属性,如果 PropertyGrid
是通过反射而不是我的覆盖 ICustomTypeDescriptor.GetProperties
方法填充的话,这些属性就会显示。
现在我知道如何获取对象的类型,然后获取 GetProperties()
,但这会返回一个 PropertyInfo
数组,而不是 ProperyDescriptor
。 那么,如何将类型的 PropertyInfo
对象转换为 PropertyDescriptor
对象,以便通过自定义 PropertyDescriptors
包含到我的集合中?
//gets the local intrinsic properties of the object
Type thisType = this.GetType();
PropertyInfo[] thisProps = thisType.GetProperties();
//this line obviously doesn't work because the propertydescriptor
//collection needs an array of PropertyDescriptors not PropertyInfo
PropertyDescriptorCollection propCOl =
new PropertyDescriptorCollection(thisProps);
I'm customizing how an object type is displayed in a PropertyGrid
by implementing ICustomTypeDescriptor
. I'm allowing the user to create their own custom properties that are stored in a single dictionary of keys and values. I'm able to create all the PropertyDescriptors
for these values and view them in the property grid. However, I also want to show all the default properties that otherwise would have shown if the PropertyGrid
was populated through reflection rather than my override ICustomTypeDescriptor.GetProperties
method.
Now I know how to get the type of the object, and then GetProperties()
, but this returns an array of PropertyInfo
not ProperyDescriptor
. So how can I convert the PropertyInfo
object of the type into PropertyDescriptor
objects to include into my collection with the custom PropertyDescriptors
?
//gets the local intrinsic properties of the object
Type thisType = this.GetType();
PropertyInfo[] thisProps = thisType.GetProperties();
//this line obviously doesn't work because the propertydescriptor
//collection needs an array of PropertyDescriptors not PropertyInfo
PropertyDescriptorCollection propCOl =
new PropertyDescriptorCollection(thisProps);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
顺便说一句:这不会包括您的
ICustomTypeDescriptor
自定义设置,但它将包括通过TypeDescriptionProvider
进行的任何自定义设置。(编辑)
其次 - 您还可以通过提供
TypeConverter
来调整PropertyGrid
- 比ICustomTypeDescriptor
或TypeDescriptionProvider
简单得多- 例如:As an aside: this won't include your
ICustomTypeDescriptor
customisations, but it will include any customisations made viaTypeDescriptionProvider
.(edit)
As a second aside - you can also tweak
PropertyGrid
by providing aTypeConverter
- much simpler than eitherICustomTypeDescriptor
orTypeDescriptionProvider
- for example: