4

我有一个具有许多属性的基类。其中包括带有元数据的扩展属性列表。这是一个自定义类的列表,其中包含 DisplayName、Description、Name、Type 和 Value 属性,以帮助 PropertyGrid。

所需的最终结果将是显示我的基类属性与上面列表中的扩展属性合并的 PropertyGrid。我不希望 PropertyGrid 将我的列表显示为单个条目,而是将每个扩展属性与我的基类属性合并。本质上,PropertyGrid 认为我的扩展属性列表是对象的第一类属性。

这可能使用反射或动态类型描述符吗?

4

1 回答 1

1

使用自定义 TypeConverter 应该相当简单。

这样的事情将是一个开始:

public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
  return true;
}

public override PropertyDescriptorCollection GetProperties(
    ITypeDescriptorContext context, object value, Attribute[] attributes)
{
  return base.GetProperties(context, value, attributes).
    Concat(TypeDescriptor.GetConverter(typeof(TheBaseType)).
    GetProperties(context, value, attributes));
}
于 2009-07-28T17:00:13.643 回答