The last article is written 03-08, distance today has been more than 10 days did not write, mainly recently too busy, but also encountered some difficulties in the work, so there is no time on the blog, is really sorry to everyone.
Today's article, I mainly bring propertyattribute in the typeconverterattribute of the explanation, first here What is the role of TypeConverterAttribute: When a property of a component is set, such as size= "60,70", the parser converts the string automatically to the type of the attribute declaration through the type converter. NET has been declared in the framework of many types of converters, which are listed in the following code. Somewhat similar to operator.
TypeConverterAttribute will also be useful in writing ASP.net server controls, and the server control's property can only be stored as a string in an ASPX page. In the case of Designtime and runtime of server controls, you must convert a string to the appropriate type.
The source code is as follows:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Globalization;
namespace ClassLibrary1
{
public class Class1:component
{
private Size _size;
public Class1 ()
{
_size = new Size ();
}
[DesignerSerializationVisibility (designerserializationvisibility.content)]
[TypeConverter (typeof (Sizeconverter))]//--Note 1, you can also write this sentence typeconverterattribute in note 2.
Public Size Size
{
get {return _size;}
set {_size = value;}
}
}
public class Sizeconverter:typeconverter//Our custom converter must inherit from the TypeConverter base class.
{
/**////<summary>
Whether the
///can be converted to the size type with string.
///</summary>
///<param name= "context" > Contexts. </param>
///<param name= "sourcetype" > Conversion Source Type. </param>
///<returns></returns>
public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourcetype)
{
if (sourcetype = = typeof (String))
{return true;}
Else
{return false;}
}
/**////<summary>
///go from string to size type.
///</summary>
///<param name= "Context" > provides component contexts, such as component.instance objects. </param>
///<param name= "Culture" > provides regional information, such as languages, time formats, currency formats </param>
///<param name= "value" ></param>
///<returns></returns>
public override Object ConvertFrom (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, Object value)
{
if (value = NULL | | value. ToString (). Length = = 0) return to new Size ();
char spliter = culture. TEXTINFO.LISTSEPARATOR[0]; Gets the delimiter for the string
string[] ss = ((string) value). Split (Spliter);
int32converter intconverter = new Int32converter (); To get a type converter,. NET defines some common type converters for us.
return to New Size ((Int32) intconverter.convertfromstring (context, culture, ss[0]),
(Int32) intconverter.convertfromstring (context, culture, SS [1]));
}
/**////<summary>
Whether the
///can be converted to a string type with size.
///</summary>
///<param name= "context" ></param>
///<param name= "destinationtype" > Conversion target type. </param>
///<returns></returns>
public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationtype)
{
if (destinationtype = = typeof (size))//If the size format is allowed to be converted to a string.
{return true;}
Else
{return false;}
}
//displayed as String type in the property window.
public override Object ConvertTo (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, O Bject value, Type destinationtype)
{
if (value = = null) return string. Empty;
if (destinationtype = = typeof (String))
{
Size size = (size) value;
TypeConverter intconverter = Typedescriptor.getconverter (typeof (Int32)); Another way of being able to type converters.
Char spliter = culture. TEXTINFO.LISTSEPARATOR[0]; Gets the delimiter for the string
return string. Join (Spliter. ToString (), New string[]{
intconverter.converttostring (context, culture, size. Length),
intconverter.converttostring (context, culture, size. Width)});
}
return string. Empty;
}
//TypeConverter There are several virtual methods, please study for yourselves.
}
//[TypeConverter (typeof (Sizeconverter))]--Note 2
public class Size
{
private Int32 _length;
private Int32 _width;
public Size (Int32 length, Int32 width)
{
_length = length;
_width = width;
}
public Size (): This (0, 0)
{
}
Public Int32 Length
{
get {return _length;}
set {_length = value;}
}
Public Int32 Width
{
get {return _width;}
set {_width = value;}
}
}
}