Type Converter and Its Implementation-1

Source: Internet
Author: User

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 );
}

}
}

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.