Used
1. String-to-value conversion
2. Conversion during design and Runtime
Implementation:
For most basic data types (such as int32, bool, Char, string, and enumeration), the. NET Framework provides the default type converter.
The default type converter is inherited from the system. componentmodel. typeconverter class and is named typeconverternameconverter.
For example, if the attribute is set to height = "150px", but the attribute is in the unit type, the problem is to convert "150px" to the unit type.
The page compiler automatically calls system. Web. UI. webcontrols. unitconverter to complete the conversion.
However, the above method can only solve part 1 of the problem. You can customize the type converter at will.
Custom Type converters can be divided into three types:
Value translation type converter,
Type converter that provides standard value list to ide
Implement the initialization of generating Properties Based on ConstructorsCodeType Converter
Introduction:
1. Value Type Converter
Converts string to attribute type.
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. componentmodel;
Using System. Globalization;
Using System. drawing;
Namespace Usercontrols
{
Public Class Testconverter: typeconverter
{
Public Override Bool Canconvertfrom (itypedescriptorcontext context, type sourcetype)
{
If (Sourcetype = Typeof (String ))
Return True ;
Else
{
Return Base. Canconvertfrom (context, sourcetype );
}
}
Public Override Object Convertfrom (itypedescriptorcontext context, cultureinfo culture, Object Value)
{
If (Value Is String )
{
String [] V = (( String ) Value). Split ( New Char [] {','} );
Return New Point ( Int . Parse (V [ 0 ]), Int . Parse (V [ 1 ]);
}
Return Base . Convertfrom (context, culture, value );
}
Public Override Object Convertize (itypedescriptorcontext context, cultureinfo culture, Object Value, type destinationtype)
{
If (Destinationtype = Typeof ( String ))
{
Return(Point) value). x+ "," +(Point) value). Y;
}
Return Base . Convertize (context, culture, value, destinationtype );
}
}
}