上一篇文章是03-08写的,距离今天已经有十多天了没有写了,主要是最近太忙了,而且在工作上遇到了一些难点,所以没有时间放在blog上,实在是对不住大家。
今天的这篇文章,我主要是带来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; }
}
}
}



下一篇文章将为大家带来Component Designer的讲解。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步