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:
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. TypeConverterAttributeTo 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. |