如何将 PropertyDescriptors 添加到类中而不是覆盖它们?

发布于 2024-08-01 15:16:48 字数 610 浏览 2 评论 0原文

如果我有一个实现 ICustomTypeDescriptor 的类,我可以重写 GetProperties() 方法,用我的自定义 PropertyDescriptors 完全替换该类的所有属性。

但是,如果我想保留类的现有属性并向类附加其他属性怎么办? 有没有办法返回一组添加到现有类属性的自定义属性描述符?

例如,我希望用户能够在我的程序中定义将显示在属性网格中的自定义属性。 自定义属性的值将存储在 Dictionary(string key, object value) 集合中,我想附加 PropertyDescriptors 来读取和写入值该集合基于键值。

我认为 IExtenderProvider 不起作用,因为它只会将一个类的属性添加到另一个类。 但我需要能够在运行时动态添加和删除属性。 我是否可以拥有一个同时实现 ICustomTypeDescriptorIExtenderProvider 类,以便可以在运行时确定它添加的属性?

If I have a class that implements ICustomTypeDescriptor I can override the GetProperties() method to completely replace all the properties of the class with my custom PropertyDescriptors.

But what if i want to keep the existing properties of the class and append additional properties to the class? Is there a way to return a set of custom property descriptors that get added to the existing class properties?

For example I want the user to be able to define custom properties in my program that will show up in a property grid. The values of the custom properties will be stored in a Dictionary(string key, object value) collection, and I want to append PropertyDescriptors that will read and write values too and from this collection based on the key values.

I don't think IExtenderProvider would work because it only adds the properties of one class to another. But I need to be able to add and remove the properties dynamically at run time. Can I have an IExtenderProvider class that also implements ICustomTypeDescriptor so that properties it adds can be figured out at runtime?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

北凤男飞 2024-08-08 15:16:48

应该能够通过添加到当前集合中:

    public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        PropertyDescriptorCollection cols = base.GetProperties(attributes);
        cols.Add(); // Add your custom property descriptor here
        return cols;
    }

Should be able to by just adding to the current collection:

    public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        PropertyDescriptorCollection cols = base.GetProperties(attributes);
        cols.Add(); // Add your custom property descriptor here
        return cols;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文