asp.net2.0 Server Control type converter _ Practical Tips

Source: Internet
Author: User

A type converter is a more important element in implementing a custom server control property. In this paper, the basic concepts and implementation methods of type converters are introduced.

1. type converter Basic Concepts

A type converter is an accessible feature implementation of a custom server control. It is primarily used to perform bidirectional conversions from a string representation to a specified type. For example, a type converter is applied to represent property values as text, to convert text entered by the user to the appropriate data type, and so on.

For most basic data types (such as Int32, Bool, Char, String, enum type, and so on), the. NET Framework has provided them with a default type converter that completes the conversion from a string to a related value and performs a validation check. The default type converter is derived from the System.ComponentModel.TypeConverter class, named Typeconverternameconverter. For example, when a developer is in the *. When setting server control properties in an ASPX file, it is found that some property values are of the base type, that is, bool, Char, Enum, int, and so on, but all are set to string types, which involves a type conversion problem. For example, a property height= "150px", the property value "150px" is a string type when set, and the property value here should be the unit type, the problem is that the page compiler must be required to convert the string "150px" to the unit type. Typically, the page compiler will automatically apply the associated type converter to complete the conversion work for attributes with a property value of the base type. In the previous example, the page compiler will automatically invoke the type converter System.Web.UI.WebControls.UnitConverter to complete the conversion of the string type to the unit type.

However, the above method solves only a few simpler type conversion problems, and in most cases the associated type conversion process is automatically completed by default. When the default type converter cannot meet the need, for example, for complex properties, there is no associated default type converter, which can be implemented by implementing a custom type converter.

The main content of this article is the custom type converter. The custom type converters can be grouped into 3 categories according to the functional classification of the custom type converters:

· A type converter for value translation;

This type converter is most common, and it mainly completes a two-way translation between a string and a worthwhile conversion, or a data type at design time and at run time. For example, the implementation is converted from a string type to an ordered pair of point type that represents an integer x and y coordinate defined in a two-dimensional plane, or to a string type. The bidirectional conversion between the string type and the Ponit type here requires a type converter that implements a value translation.

· A type converter that provides a list of standard values to the property window;

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

· Initializes a type converter for the property to generate code at run time;

The. 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 initialization code based on constructors. 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 logic to configure the type of the property and the value of the constructor.

Implementing all 3 types of converters requires that 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 is a brief introduction to the basics of TypeConverter and its subclasses.

The TypeConverter class provides a common way to convert a type of value to another type and to access standard values and child properties. The class includes multiple member methods. For creating a custom type converter, readers should understand the following common methods:

(1) CanConvertFrom method: Returns whether the converter can convert an object of one type to 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 a 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 a list.

(7) GetStandardValues method: Returns a collection of standard values for the data type that this type converter is designed for.

The TypeConverter class is the basis for implementing a type converter. To support the default type conversion feature, ASP.net 2.0 also extends the TypeConverter class, which has several derived classes built into it. For example, Charconverter, Datetimeconverter, Expandableobjectconverter, Enumconverter and so on. They can also help developers create custom type converters while completing type conversions, for example, by creating a type converter that inherits from the Expandableobjectconverter class, which provides a type converter to implement transformations between extensible objects and various other representations. This simplifies the process of creating a type converter (relative to the TypeConverter base class).

Also, in the use of an existing type converter procedure, be aware that you should not access the type converter directly at any time. Instead, call the appropriate converter by using TypeDescriptor.

After you implement a type converter, you can apply a type converter using the following methods.

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

The above code shows how the type converter is applied. It notifies MyClass to use a type converter named MyClassConverter. This example assumes that MyClassConverter has been implemented in a different location. In the application process, it is necessary to note that metadata attribute TypeConverter typically applies to complex properties or data members to associate with a type converter. If you apply TypeConverter to a type, you do not have to apply it again to a property or data member of that type.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.