C# 获取类型的默认PropertyDescriptor
我正在通过实现C# 获取类型的默认PropertyDescriptor,c#,propertygrid,propertyinfo,propertydescriptor,C#,Propertygrid,Propertyinfo,Propertydescriptor,我正在通过实现ICustomTypeDescriptor自定义对象类型在PropertyGrid中的显示方式。我允许用户创建自己的自定义属性,这些属性存储在一个键和值字典中。我能够为这些值创建所有的PropertyDescriptor,并在属性网格中查看它们。但是,我还想显示所有默认属性,如果PropertyGrid是通过反射而不是我的overrideICustomTypeDescriptor.GetProperties方法填充的,则会显示这些属性 现在我知道了如何获取对象的类型,然后是GetP
ICustomTypeDescriptor
自定义对象类型在PropertyGrid
中的显示方式。我允许用户创建自己的自定义属性,这些属性存储在一个键和值字典中。我能够为这些值创建所有的PropertyDescriptor
,并在属性网格中查看它们。但是,我还想显示所有默认属性,如果PropertyGrid
是通过反射而不是我的overrideICustomTypeDescriptor.GetProperties
方法填充的,则会显示这些属性
现在我知道了如何获取对象的类型,然后是GetProperties()
,但这将返回一个PropertyInfo
数组,而不是propertyDescriptor
。那么,如何将类型的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);
顺便说一下:这不包括您的ICustomTypeDescriptor
自定义,但包括通过TypeDescriptionProvider
进行的任何自定义
(编辑)
另一方面,您还可以通过提供比ICustomTypeDescriptor
或TypeDescriptionProvider
简单得多的TypeConverter
来调整PropertyGrid
,例如:
[TypeConverter(typeof(FooConverter))]
class Foo { }
class FooConverter : ExpandableObjectConverter
{
public override PropertyDescriptorCollection GetProperties(
ITypeDescriptorContext context, object value, Attribute[] attributes)
{
// your code here, perhaps using base.GetPoperties(
// context, value, attributes);
}
}
[TypeConverter(typeof(FooConverter))]
class Foo { }
class FooConverter : ExpandableObjectConverter
{
public override PropertyDescriptorCollection GetProperties(
ITypeDescriptorContext context, object value, Attribute[] attributes)
{
// your code here, perhaps using base.GetPoperties(
// context, value, attributes);
}
}