views:

2356

answers:

1

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.

+9  A: 

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
Marc Gravell