获取类型的默认 PropertyDescriptor

发布于 2024-07-16 15:23:41 字数 960 浏览 6 评论 0原文

我通过实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的位置 2024-07-23 15:23:41
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(thisType);

顺便说一句:这不会包括您的 ICustomTypeDescriptor 自定义设置,但它包括通过 TypeDescriptionProvider 进行的任何自定义设置。

(编辑)
其次 - 您还可以通过提供 TypeConverter 来调整 PropertyGrid - 比 ICustomTypeDescriptorTypeDescriptionProvider 简单得多- 例如:

[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);
    }
}
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(thisType);

As an aside: this won't include your ICustomTypeDescriptor customisations, but it will include any customisations made via TypeDescriptionProvider.

(edit)
As a second aside - you can also tweak PropertyGrid by providing a TypeConverter - much simpler than either ICustomTypeDescriptor or TypeDescriptionProvider - for example:

[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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文