Eri*_*tas 9 c# propertygrid propertyinfo propertydescriptor
我正在PropertyGrid
通过实现自定义对象类型的显示方式ICustomTypeDescriptor
.我允许用户创建自己的自定义属性,这些属性存储在单个键和值字典中.我能够PropertyDescriptors
为这些值创建所有值并在属性网格中查看它们.但是,我还想显示所有默认属性,如果PropertyGrid
通过反射填充而不是覆盖ICustomTypeDescriptor.GetProperties
方法,则会显示这些属性.
现在我知道如何获取对象的类型,然后GetProperties()
,但这会返回一个PropertyInfo
not 数组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)