Type Converters


A type converter is a class that derives from System.ComponentModel.TypeConverter and performs conversions from a given type to other types and vice versa. Type converters are used both at design time and at run time, as we described in Chapter 10.

At design time, the property browser uses type converters for the following purposes:

  • To convert the values of properties that are not of the String type to string representations that can be displayed in the property browser. Conversely, the property browser uses type converters to convert user -entered text strings into objects that represent the appropriate property types. In Chapter 10, we described examples ” MapCircle ­Converter and MapRectangleConverter ” that show how to implement a type converter that performs string-to-value conversions.

  • To provide an expand/collapse UI for subproperties . System.ComponentModel.ExpandableObjectConverter provides the base implementation for this functionality. You can extend the ExpandableObjectConverter class, as you saw in the MapShapeConverter class in Chapter 10.

  • To provide a drop-down list of values for a design-time property in a data-bound control. In Chapter 16, "Data-Bound Controls," we will utilize a type converter that provides this feature.

The control persister, which generates HTML corresponding to controls, uses type converters to persist properties in the form of declarative syntax in an .aspx page. We examined property persistence in Chapter 10 and explained that properties are either persisted on the control's tags or nested within the control's tags.

Because the examples in Chapter 10 and Chapter 16 cover the cases described in the preceding list, we will not provide examples of type converters in this chapter.

TypeConverterAttribute

To associate a type converter with a type, you must apply the System.ComponentModel.TypeConverterAttribute metadata attribute to the type, as we described in Chapter 10. You can also apply TypeConverterAttribute to a property to associate a type converter with that property. The second technique is useful when the property type does not have an associated type converter or when you want to override the existing type converter associated with the property type.

The constructor of TypeConverterAttribute is overloaded. The attribute usage syntax depends on the relative assembly locations of the type and the type converter. The most common usage scenario is where the type converter and the type it is associated with are in the same assembly, or when you are using a built-in type converter from the .NET Framework class library. In those cases, you can pass an early-bound type reference into the constructor, as shown in the following example:

 [ TypeConverter(typeof(MapCircleConverter)) ] publicclassMapCircle{...} 

When the reference to the type converter can be resolved at compile time, you should use the syntax shown in this example. The scenarios that require a late-bound reference (and the corresponding attribute syntax) are similar to those described in the "DesignerAttribute" section later in this chapter.



Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net