获取类型的默认PropertyDescriptors

Eri*_*tas 9 c# propertygrid propertyinfo propertydescriptor

我正在PropertyGrid通过实现自定义对象类型的显示方式ICustomTypeDescriptor.我允许用户创建自己的自定义属性,这些属性存储在单个键和值字典中.我能够PropertyDescriptors为这些值创建所有值并在属性网格中查看它们.但是,我还想显示所有默认属性,如果PropertyGrid通过反射填充而不是覆盖ICustomTypeDescriptor.GetProperties方法,则会显示这些属性.

现在我知道如何获取对象的类型,然后GetProperties(),但这会返回一个PropertyInfonot 数组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);
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 15

PropertyDescriptorCollection props = TypeDescriptor.GetProperties(thisType);
Run Code Online (Sandbox Code Playgroud)

顺便说一句:这不包括你ICustomTypeDescriptor的定制,但包括通过所做的任何的定制TypeDescriptionProvider.

(编辑)作为第二个旁边 - 您还可以PropertyGrid通过提供TypeConverter- 比任何一个ICustomTypeDescriptor或更简单TypeDescriptionProvider- 进行调整,例如:

[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);
    }
}
Run Code Online (Sandbox Code Playgroud)