ICustomTypeDescriptor, TypeDescriptionProvider, TypeConverter, and UITypeEditor

Eric Anastas picture Eric Anastas · Apr 15, 2009 · Viewed 9.7k times · Source

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.

Answer

Marc Gravell picture Marc Gravell · Apr 15, 2009

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