ICustomTypeDescriptor、TypeDescriptionProvider、TypeConverter 和 UITypeEditor

发布于 2024-07-17 06:14:18 字数 1134 浏览 6 评论 0原文

我试图全面了解如何使用 ICustomTypeDescriptor、TypeDescriptionProvider、TypeConverter 和 UITypeEditor 来更改 PropertyGrid 的显示方式以及与对象的交互方式。

有人可以告诉我这是否正确,或者我是否遗漏了任何主要概念或要点? 我真的只是想了解为什么以及何时使用每个类。

ICustomTypeDescriptor

  • 在类中实现此接口完全覆盖类的本机属性,并将它们替换为 ICustomTypeDescriptor.GetProperties() 返回的 PropertyDescriptors

TypeDescriptionProvider

  • 用于扩展现有属性类的
  • TypeDescriptionProvider 通过 TypeDescriptionProvider 属性附加到类 TypeDescriptionProvider 的
  • GetTypeDescriptor() 方法返回要附加到该类型的现有属性的 ICustomTypeDescriptor。
  • PropertyGrid 将显示通过 Reflection 找到的类的属性,以及通过 TypeDescriptionProvider 添加到类的属性

TypeConverter

  • 在类型之间进行转换
  • 就使用 PropetyGrid 用于在复杂类型和基本类型之间进行转换而言可以在属性网格中显示/编辑。
  • TypeConverter 的 GetStandard 值方法还可用于显示属性网格中的可能值列表

UITypeEditor

  • 定义用于操作复杂类型属性的自定义编辑器。
  • 通过属性与属性关联。

因此,ICustomTypeDescriptor 是一个 TypeDescription 提供程序,用于添加/更改/替换对象的整个属性。 TypeConverter 和 UITypeEditor 应用于各个属性并控制这些特定属性的交互方式。

I'm trying to get an overall understanding of how you use ICustomTypeDescriptor, TypeDescriptionProvider, TypeConverter, and UITypeEditor to change how a PropertyGrid displays and interfaces with an object.

Can someone tell me if this is right, or if I missed any major concepts or points? I'm really just trying to understand why and when you would use each class.

ICustomTypeDescriptor

  • Implementing this interface in a class totaly overrides the native properties of a class and replaces them with the PropertyDescriptors returned by ICustomTypeDescriptor.GetProperties()

TypeDescriptionProvider

  • Used to extend on the existing properties of a class
  • A TypeDescriptionProvider is appended to a class through a TypeDescriptionProvider attribute
  • The GetTypeDescriptor() method of the TypeDescriptionProvider returns an ICustomTypeDescriptor to be appended to the existing properties of the type.
  • A PropertyGrid will show both the properties of the class found through Reflection, and the properties added to the class through the TypeDescriptionProvider

TypeConverter

  • Converts between types
  • In terms of using a PropetyGrid used to convert between complex types and primitive types that can be displayed/edited in the property grid.
  • The GetStandard values method of a TypeConverter can also be used to show a list of possible values in the propertygrid

UITypeEditor

  • Defines a custom editor for manipulating a property of a complex type.
  • Associated with a property through an attribute.

So ICustomTypeDescriptor an TypeDescription provider are used to add/change/replace entire properties of objects. TypeConverter and UITypeEditor are applied to individual properties and control how those specific properties are interfaced with.

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

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

发布评论

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

评论(1

孤凫 2024-07-24 06:14:18

调整:

  • TypeDescriptionProvider

    • 仍然替换元数据(不扩展)
    • 也可以添加为TypeDescriptor.AddProvider
    • 可以应用于每个类型以及每个实例,从而可以应用于列表而无需实现 ITypedList
  • TypeConverter

    • 对于PropertyGrid来说,这也是用于获取元数据的机制; 请注意,ExpandableObjectConverter 只是委托给 TypeDescriptor.GetProperties,但情况并非总是如此
    • 还负责通过更改重新创建不可变对象(例如结构)
  • <代码>UITypeEditor

    • 还负责在 PropertyGrid 中绘制预览框

附加:

  • IExtenderProvider - 附加属性; 这可能是您与 TypeDescriptionProvider ITypedList 混淆的地方
  • - 大致上是 ICustomTypeDescriptor 的双胞胎,但对于列表而言; 可以通过使用 TypeDescriptionProvider 和任何 IList 上的非对象索引器来避免,即 public T this[int index] {get;}
  • IListSource - 提供数据源和数据之间的间接连接; 例如,DataTable 实现 IListSource,在请求时返回 DefaultView

Tweaks:

  • TypeDescriptionProvider

    • still replaces metadata (not extends)
    • can also be added be TypeDescriptor.AddProvider
    • can be applied per-type as well as per-instance, making it possible to apply to lists without having to implement ITypedList
  • TypeConverter

    • for PropertyGrid, this is also the mechanism used to obtain metadata; note that ExpandableObjectConverter simply delegates to TypeDescriptor.GetProperties, but this is not always the case
    • also responsible for re-creating immutable objects (such as structs) with changes
  • UITypeEditor

    • also responsible for painting the preview box in PropertyGrid

Additional:

  • IExtenderProvider - appends properties; this may be what you were getting confused with TypeDescriptionProvider
  • ITypedList - broadly the twin of ICustomTypeDescriptor, but for lists; can be avoided by use of TypeDescriptionProvider and a non-object indexer on any IList, i.e. public T this[int index] {get;}
  • IListSource - provides indirection between a data-source and the data; for example, a DataTable implements IListSource, returning DefaultView when requested
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文