上一篇文章我已經介紹了TypeConverterAttribute元數據的作用,本文將通過代碼向你展示具體的實 現。在這個例子中,我要給控件添加一個復雜的屬性,這個屬性對這個控件沒有什麼功用,純粹是為了演 示,有些牽強附會了。
現在在前一篇文章中的創建的控件代碼中添加一個Scope屬性:
[Browsable(true)]
public Scope Scope
{
get
{
return _scope;
}
set
{
_scope = value;
}
}
這個屬性的類型是Scope類,代碼如下:
public class Scope
{
private Int32 _min;
private Int32 _max;
public Scope()
{
}
public Scope(Int32 min, Int32 max)
{
_min = min;
_max = max;
}
[Browsable(true)]
public Int32 Min
{
get
{
return _min;
}
set
{
_min = value;
}
}
[Browsable(true)]
public Int32 Max
{
get
{
return _max;
}
set
{
_max = value;
}
}
}
添加完屬性後,build控件工程,然後在測試的工程裡選中添加的控件,然後在屬性浏覽器裡觀察它的 屬性,發現Scope屬性是灰的,不能編輯。前一篇文章提到了,在屬性浏覽器裡可以編輯的屬性都是有類 型轉換器的,而.NET框架為基本的類型和常用的類型都提供了默認的類型轉換器。接下來我們為Scope類 添加一個類型轉換器,以便這個屬性能夠被編輯,而且也可以在源代碼文件裡自動生成相應的代碼。下面 是類型轉換器的代碼:
public class ScopeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type
sourceType)
{
if (sourceType == typeof(String)) return true;
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type
destinationType)
{
if (destinationType == typeof(String)) return true;
if (destinationType == typeof(InstanceDescriptor)) return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type destinationType)
{
String result = "";
if (destinationType == typeof(String))
{
Scope scope = (Scope)value;
result = scope.Min.ToString()+"," + scope.Max.ToString();
return result;
}
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(Scope).GetConstructor(new Type[]
{typeof(Int32),typeof(Int32) });
Scope scope = (Scope)value;
return new InstanceDescriptor(ci, new object[] {
scope.Min,scope.Max });
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
String[] v = ((String)value).Split(',');
if (v.GetLength(0) != 2)
{
throw new ArgumentException("Invalid parameter
format");
}
Scope csf = new Scope();
csf.Min = Convert.ToInt32(v[0]);
csf.Max = Convert.ToInt32(v[1]);
return csf;
}
return base.ConvertFrom(context, culture, value);
}
}
現在我們為類型提供類型轉換器,我們在類型前面添加一個TypeConverterAttribute,如下:
[TypeConverter(typeof(ScopeConverter))] public class Scope
添加完以後build工程,然後切換到測試工程,選中控件,在屬性浏覽器裡查看屬性,現在的Scope屬 性可以編輯了,如下圖所示:

我們修改默認的值,然後看看Form設計器為我們生成了什麼代碼:
this.myListControl1.BackColor = System.Drawing.SystemColors.ActiveCaptionText; this.myListControl1.Item.Add(1); this.myListControl1.Item.Add(2); this.myListControl1.Item.Add(3); this.myListControl1.Item.Add(6); this.myListControl1.Item.Add(8); this.myListControl1.Item.Add(9); this.myListControl1.Location = new System.Drawing.Point(12, 34); this.myListControl1.Name = "myListControl1"; this.myListControl1.Scope = new CustomControlSample.Scope(10, 200); this.myListControl1.Size = new System.Drawing.Size(220, 180); this.myListControl1.TabIndex = 1; this.myListControl1.Text = "myListControl1";
關鍵是這一行this.myListControl1.Scope = new CustomControlSample.Scope(10, 200),Scope類的 類型轉換器為屬性提供了實例化的代碼。