Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

c# - ICustomTypeDescriptor, TypeDescriptionProvider, TypeConverter, and 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...