A type converter is an important part in the process of customizing server control attributes. This article introduces the basic concepts and implementation methods of the type converter.
1. Basic concepts of type converter
A type converter is an auxiliary function implementation of custom server controls. It is mainly used to perform bidirectional conversion from string representation to the specified type. For example, Data Type converters are applied to represent attribute values in the form of text and convert user input text to the corresponding data type.
For most basic data types (such as Int32, Bool, Char, String, and enumeration ),. the net framework already provides them with default Type converters that convert from strings to related values and perform verification checks. The default type converter is derived from the System. ComponentModel. TypeConverter class and is named TypeConverterNameConverter. For example *. when setting Server Control Properties in the ASPx file, you will find that some attribute values are of the basic type, such as Bool, Char, Enum, and Int. However, all attribute values are of the String type, this involves a type conversion problem. For example, the attribute Height = "150px" and the attribute value "150px" are set to the String type, and the attribute value here should be of the Unit type, the problem is that the page compiler must convert the string "150px" to the Unit type. Generally, for properties whose property values are of the basic type, the page compiler automatically applies the relevant type converter to complete the conversion. In the previous example, the page compiler will automatically call the type converter System. Web. UI. WebControls. UnitConverter to convert the String type to the Unit type.
However, the above method can only solve a few simple type conversion problems, and in most cases, the related type conversion process is automatically completed by default. If the default type converter cannot meet the requirements, for example, if there is no associated default type converter for complex attributes, You can implement a custom type converter.
Custom type converter is the focus of this article. According to the function classification of the custom type converter, you can divide the custom type converter into three types:
· Value translation type converter;
This type converter is the most common, mainly used for string-to-value conversion, or bidirectional translation between data types at design time and runtime. For example, you can convert the String type to the ordered Point type that represents the points defined in the two-dimensional plane, the integer X and Y coordinates, or convert the Point type to the String type. A value translation type converter is required for Bidirectional conversion between the String type and the Ponit type.
· Type converter that provides the standard value list to the attribute window;
The Control Properties window is included in Visual Studio 2005. The type converter can provide a value list for the type of the control in the Property Window. When you click the value list, you can easily set the attribute value in the drop-down list.
· Type converter for generating code for property initialization at runtime;
. NET Framework provides the ability to generate dynamic attribute initialization code (this code initializes attributes at runtime) during design. Developers can build a type converter that generates initialization code based on constructors. To configure the type attributes at runtime, these types of converters can dynamically generate the constructor code using the values set during design. The type converter implements logic to configure the type of the attribute and the value of the constructor.
To implement the above three types of converters, the class of the custom type converter must inherit from the System. ComponentModel. TypeConverter base class or the existing subclass of the TypeConverter class. The following describes the basic situation of TypeConverter and its sub-classes.
The TypeConverter class provides a unified method to convert the value type to other types, as well as access standard values and sub-attributes. This class includes multiple Member methods. For creating a custom type converter, you should understand the following common methods:
(1) CanConvertFrom method: returns whether the converter can convert a type of object to the type of the converter.
(2) ConvertFrom method: Convert the given value to the type of the converter.
(3) canconvertor method: returns whether the converter can convert the object to a specified type.
(4) convertor method: converts a specified value object to a specified type.
(5) IsValid method: whether the returned value object is valid for this type.
(6) GetStandardValuesSupported method: returns whether this object supports the standard value set that can be selected from the list.
(7) GetStandardValues method: returns a set of standard values of the Data Type designed for use by this type converter.
The TypeConverter class is the basis for implementing the type converter. To support the default type conversion function, ASP. NET 2.0 also extends the TypeConverter class and has multiple built-in Derived classes. For example, CharConverter, DateTimeConverter, ExpandableObjectConverter, and EnumConverter. They can also help developers create custom Type converters while completing type conversion. For example, they can create Type converters inherited from the ExpandableObjectConverter class, this base class provides a type converter for conversion between extensible objects and other forms of representation. This simplifies the process of creating a type converter (relative to the TypeConverter base class.
In addition, when using an existing type converter, note that do not directly access the type converter at any time. Instead, use TypeDescriptor to call the appropriate converter.
After the type converter is implemented, you can use the following method to apply the type converter.
[TypeConverter(typeof(MyClassConverter))]
public class MyClass {
// Insert code here.
}
The above code shows the application method of the type converter. It notifies MyClass to use a type converter named MyClassConverter. This example assumes that MyClassConverter has been implemented elsewhere. During the application process, you must note that the metadata attribute TypeConverter is usually used for complex attributes or data members to associate it with the type converter. If TypeConverter is applied to a type, it is not necessary to apply it again to properties or data members of the type.