如何将 PropertyDescriptors 添加到类中而不是覆盖它们?
如果我有一个实现 ICustomTypeDescriptor 的类,我可以重写 GetProperties() 方法,用我的自定义 PropertyDescriptors 完全替换该类的所有属性。
但是,如果我想保留类的现有属性并向类附加其他属性怎么办? 有没有办法返回一组添加到现有类属性的自定义属性描述符?
例如,我希望用户能够在我的程序中定义将显示在属性网格中的自定义属性。 自定义属性的值将存储在 Dictionary(string key, object value) 集合中,我想附加 PropertyDescriptors 来读取和写入值该集合基于键值。
我认为 IExtenderProvider 不起作用,因为它只会将一个类的属性添加到另一个类。 但我需要能够在运行时动态添加和删除属性。 我是否可以拥有一个同时实现 ICustomTypeDescriptor 的 IExtenderProvider 类,以便可以在运行时确定它添加的属性?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该能够通过添加到当前集合中:
Should be able to by just adding to the current collection: