Type converter for ASP.NET2.0 server control

  • 2020-05-05 11:08:33
  • OfStack

The type converter is an important part in the process of implementing custom server control properties. This article will introduce the basic concepts and implementation methods of type converters.

1. Basic concept of type converter

Type converters are auxiliary implementations of custom server controls. It is primarily used to perform two-way conversions from the string representation to the specified type. For example, type converters are applied to representing property values as text, converting text entered by the user to the appropriate data type, and so on.

For most basic data types (such as Int32, Bool, Char, String, enumerated types, and so on), the.net framework already provides them with default type converters that perform conversion from strings to relevant values and perform validation checks. The default type converter is derived from the System.ComponentModel.TypeConverter class, named TypeConverterNameConverter. For example, when developers set server control properties in the *.ASPx file, they will find that some property values are of basic types, Bool, Char, Enum, Int, etc., but they are all set to String, which raises a type conversion issue. For example, if the property Height="150px", and the property value "150px" is of type String when set, and the property value here should be of type Unit, the problem is that the page compiler must be required to convert the string "150px" to type Unit. Typically, the page compiler automatically applies the associated type converter to properties whose values are of basic types. In the case, just page. The compiler will automatically call type converter System Web. UI. WebControls. UnitConverter complete String type and mutual transformation between Unit type.

However, the above approach can only solve a few of the simpler type conversion problems, and in most cases, the relevant type conversion process is done automatically by default. When the default type converter is not sufficient, for example, in the case of complex properties, the default type converter is not associated, it can be implemented by implementing a custom type converter.

Custom type converters are the focus of this article. According to the function classification of the custom type converter, the custom type converter can be divided into three types:

· type converter for value translation;

This type converter is the most common and is primarily used to convert from strings to values, or for two-way translation between data types at design time and run time. For example, an String type can be converted to an Point type that represents an ordered pair of the integer X and Y coordinates of a point defined in a two-dimensional plane, or from an Point type to an String type. A two-way conversion between String and Ponit types here requires a type converter for value translation.

· type converters that provide a list of standard values to the properties window;

The control property window is included in Visual Studio 2005. A type converter can provide a list of values for the type of a control in a property window. When the developer clicks on the value list, it is convenient to set the value of the property in the drop-down list.

· type converters that generate code at run time for properties to be initialized;

.NET Framework provides the ability to generate dynamic property initialization code at design time, which initializes properties at run time. Developers can build a type converter that produces construction-based initialization code. To configure type properties at run time, these type converters can dynamically generate constructor code using values set at design time. The type converter implements the logic to configure the type of the property and the value of the constructor.

Implementing all three types of converters requires that the class that defines the type converter must inherit from the System.ComponentModel.TypeConverter base class, or from an existing subclass of the TypeConverter class. Here is a brief overview of TypeConverter and its subclasses.

The TypeConverter class mainly provides a unified way to convert the types of values to other types and to access the standard values and child properties. The class includes multiple member methods. For creating custom type converters, the reader should be aware of the following common methods:

(1) CanConvertFrom method: returns whether the converter can convert an object of a type into the type of this converter.

(2) ConvertFrom method: converts the given value to the type of this converter.

(3) CanConvertTo method: returns whether this converter can convert the object to the specified type.

(4) ConvertTo method: converts the given value object to the specified type.

(5) IsValid method: returns whether the given value object is valid for this type.

(6) GetStandardValuesSupported method: returns whether this object supports a standard set of values that can be selected from the list.

(7) GetStandardValues method: returns a set of standard values for the data types for which this type converter is designed.

The TypeConverter class is the basis for implementing a type converter. To support the default type conversion, ASP.NET 2.0 also extends the TypeConverter class with several derived classes built in. For example, CharConverter, DateTimeConverter, ExpandableObjectConverter, EnumConverter, etc. They also help developers create custom type converters, for example, from the ExpandableObjectConverter class, which provides type converters to convert between extensible objects and various other representations. This simplifies the process of creating a type converter (as opposed to an TypeConverter base class).

Also, when using an existing type converter, be careful: do not access the type converter directly at any time. Instead, call the appropriate converter by using TypeDescriptor.

After implementing the type converter, you can apply the type converter using the following method.

[TypeConverter(typeof(MyClassConverter))]
public class MyClass {
// Insert code here
}

The above code shows the application method of the type converter. It informs MyClass to use a type converter named MyClassConverter. This example assumes that MyClassConverter has been implemented elsewhere. It is important to note during application that the metadata attribute TypeConverter is usually applied to a complex property or data member to associate it with a type converter. If TypeConverter is applied to a type, it is not necessary to apply it again to attributes or data members of that type.


Related articles: