【问题标题】:How do I add PropertyDescriptors to a class rather then override them?如何将 PropertyDescriptors 添加到类而不是覆盖它们?
【发布时间】:2009-08-10 19:49:58
【问题描述】:

如果我有一个实现 ICustomTypeDescriptor 的类,我可以重写 GetProperties() 方法,用我的自定义 PropertyDescriptors。

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

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

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

【问题讨论】:

    标签: propertygrid propertydescriptor iextenderprovider


    【解决方案1】:

    应该可以通过添加到当前集合中:

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

    【讨论】:

      猜你喜欢
      • 2018-12-13
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      相关资源
      最近更新 更多