前面章節說明了一個重要的類TypeConverter,有些對象需要提供自定義的描述的時候,TypeConverter可能就不滿足,在那些情況下,需要實現自定義的描述呢, 比如以下需求:
- 當對象需要動態類型信息時,需要自描述的時候。
- COM 對象的類型信息,COM 對象不支持屬性或屬性,需要使用IcustomTypeDescriptor類封裝。
本章我們根據這兩個需求,分別介紹當前接口的應用。
為了實現對象可以當組件一樣使用,我讓類繼承了Component類,代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AlbertControlExample.Controls
{
/// <summary>
/// 定義一個組件
/// </summary>
public class CustomDef : Component, ICustomTypeDescriptor
{
public CustomDef()
{
Left = 0;
Top = 0;
}
public CustomDef(int left, int top)
{
this.Left = left;
this.Top = top;
}
public double Left { get; set; }
public double Top { get; set; }
/// <summary>
/// 獲取當前attributes集合
/// </summary>
/// <returns></returns>
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}
public string GetClassName()
{
return "類名稱";
}
public string GetComponentName()
{
return "組件名稱";
}
/// <summary>
/// 當前對象的TypeConverter
/// </summary>
/// <returns></returns>
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}
/// <summary>
/// 返回當前的事件描述器
/// </summary>
/// <returns></returns>
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
/// <summary>
/// 返回當前的默認屬性
/// </summary>
/// <returns></returns>
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}
/// <summary>
/// 返回當前的編輯器
/// </summary>
/// <param name="editorBaseType"></param>
/// <returns></returns>
public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
/// <summary>
/// 返回當前的事件集合
/// </summary>
/// <returns></returns>
public EventDescriptorCollection GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}
/// <summary>
/// 返回當前的事件描述集合
/// </summary>
/// <param name="attributes"></param>
/// <returns></returns>
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}
/// <summary>
/// 返回當前的屬性集合
/// </summary>
/// <returns></returns>
public PropertyDescriptorCollection GetProperties()
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this, true);
return properties;
}
/// <summary>
/// 返回當前的屬性描述集合
/// </summary>
/// <param name="attributes"></param>
/// <returns></returns>
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this, attributes, true);
return properties;
}
/// <summary>
/// 返回當前屬性的宿主
/// </summary>
/// <param name="pd"></param>
/// <returns></returns>
public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
}
}
我們分別說明每個函數的作用和意義。
1、顯示一些名稱
public string GetClassName() { return "類名稱"; } public string GetComponentName() { return "組件名稱"; }
這兩個主要用於顯示的目的,如圖

2、默認屬性的默認事件
/// <summary> /// 返回當前的默認事件 /// </summary> /// <returns></returns> public EventDescriptor GetDefaultEvent() { return TypeDescriptor.GetDefaultEvent(this, true); } /// <summary> /// 返回當前的默認屬性 /// </summary> /// <returns></returns> public PropertyDescriptor GetDefaultProperty() { PropertyDescriptor propertyDescriptor = TypeDescriptor.CreateProperty(typeof(CustomDef),"Left",typeof(double)); return propertyDescriptor; }
這兩個函數很特殊,其實就是顯示默認的屬性和雙擊控件默認的生成事件,如下圖,優先顯示的屬性

3、當前的屬性集合和事件集合
/// <summary> /// 返回當前的事件集合 /// </summary> /// <returns></returns> public EventDescriptorCollection GetEvents() { return TypeDescriptor.GetEvents(this, true); } /// <summary> /// 返回當前的事件描述集合 /// </summary> /// <param name="attributes"></param> /// <returns></returns> public EventDescriptorCollection GetEvents(Attribute[] attributes) { return TypeDescriptor.GetEvents(this, attributes, true); } /// <summary> /// 返回當前的屬性集合 /// </summary> /// <returns></returns> public PropertyDescriptorCollection GetProperties() { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this, true); return properties; } /// <summary> /// 返回當前的屬性描述集合 /// </summary> /// <param name="attributes"></param> /// <returns></returns> public PropertyDescriptorCollection GetProperties(Attribute[] attributes) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this, attributes, true); return properties; }
4、編輯器對象
用於獲取當前的對象的屬性列表和事件列表,也就是我們所有顯示的列表對象
/// <summary> /// 返回當前的編輯器 /// </summary> /// <param name="editorBaseType"></param> /// <returns></returns> public object GetEditor(Type editorBaseType) { return TypeDescriptor.GetEditor(this, editorBaseType, true); }
用於獲取當前的類型編輯器,這個在下一章會詳細介紹。
其他函數,比較簡單,很容易理解。
