今天的這篇文章,我主要是帶來propertyattribute里的typeconverterattribute的講解,首先在這里講講typeconverterattribute的作用是什么:當component的某個property被設置時,如size="60,70",解析器會通過類型轉化器,把這個字符串自動轉換為屬性聲明的類型。.net的框架中已經聲明了很多的類型轉化器,下面的代碼中有列舉到。有點類似于operator。
同時在asp.net服務器控件的編寫中typeconverterattribute也將會非常有用,服務器控件的property只能以string形式保存在aspx頁面里,而在服務器控件的designtime和runtime時必須要把string轉換為相應的類型。
源代碼如下:
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))] // —— 注1,也可以把這句typeconverterattribute寫在注2處。
public size size
{
get { return _size; }
set { _size = value; }
}
}
public class sizeconverter : typeconverter // 我們自定義的converter必須繼承于typeconverter基類。
{
/**//// <summary>
/// 是否能用string轉換到size類型。
/// </summary>
/// <param name="context">上下文。</param>
/// <param name="sourcetype">轉換源的type。</param>
/// <returns></returns>
public override bool canconvertfrom(itypedescriptorcontext context, type sourcetype)
{
if (sourcetype == typeof(string))
{ return true; }
else
{ return false; }
}
/**//// <summary>
/// 從string轉到size類型。
/// </summary>
/// <param name="context">提供component的上下文,如component.instance對象等。</param>
/// <param name="culture">提供區域信息,如語言、時間格式、貨幣格式等</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 new size();
char spliter = culture.textinfo.listseparator[0]; // 得到字符串的分隔符
string[] ss = ((string)value).split(spliter);
int32converter intconverter = new int32converter(); // 得到類型轉換器,.net中為我們定義了一些常見的類型轉換器。
return new size((int32)intconverter.convertfromstring(context, culture, ss[0]),
(int32)intconverter.convertfromstring(context, culture, ss[1]));
}
/**//// <summary>
/// 是否能用size轉換到string類型。
/// </summary>
/// <param name="context"></param>
/// <param name="destinationtype">轉換目標的類型。</param>
/// <returns></returns>
public override bool canconvertto(itypedescriptorcontext context, type destinationtype)
{
if (destinationtype == typeof(size)) // 如果是size格式,則允許轉成string。
{ return true; }
else
{ return false; }
}
// 在property窗口中顯示為string類型。
public override object convertto(itypedescriptorcontext context, system.globalization.cultureinfo culture, object value, type destinationtype)
{
if (value == null) return string.empty;
if (destinationtype == typeof(string))
{
size size = (size)value;
typeconverter intconverter = typedescriptor.getconverter(typeof(int32)); // 能到類型轉換器的另一種方式。
char spliter = culture.textinfo.listseparator[0]; // 得到字符串的分隔符
return string.join(spliter.tostring(), new string[]{
intconverter.converttostring(context, culture, size.length),
intconverter.converttostring(context, culture, size.width)});
}
return string.empty;
}
// typeconverter還有幾個虛方法,請大家自己研究。
}
// [typeconverter(typeof(sizeconverter))] —— 注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; }
}
}
}

