反射手册笔记 6.NET组件模型
1.NET组件的层次:
2.获取元数据,以下3个方法分别获取Type,Event,Properties的元数据:
对于Type,通过TypeDescriptor静态类,获取其默认事件,默认属性,以及该类型所带的所有Attribute(通过枚举取得)
可以通过TypeDescriptor.GetEvents静态方法获取事件集合EventDescriptorCollection,遍历其成员。——Properties同理
TypeConverter,负责将值转换成其它数据类型,反之亦然
TypeDescriptor.GEtEditor()方法,需要参数:属性的数据类型,编辑器的基本数据类型(UITypeEditor是所有编辑器类的基类)。
2.获取元数据,以下3个方法分别获取Type,Event,Properties的元数据:
对于Type,通过TypeDescriptor静态类,获取其默认事件,默认属性,以及该类型所带的所有Attribute(通过枚举取得)
可以通过TypeDescriptor.GetEvents静态方法获取事件集合EventDescriptorCollection,遍历其成员。——Properties同理
public static void MyDisplayTypeInfo(Type theType)
{
Console.WriteLine(theType.FullName);
EventDescriptor defaultEvent = TypeDescriptor.GetDefaultEvent(theType);
if (defaultEvent != null)
{
Console.WriteLine(defaultEvent.Name);
}
PropertyDescriptor defaultProperty = TypeDescriptor.GetDefaultProperty(theType);
if (defaultProperty != null)
{
Console.WriteLine(defaultProperty.Name);
}
Console.WriteLine("Attributes:");
AttributeCollection attribs = TypeDescriptor.GetAttributes(theType);
IEnumerator iter = attribs.GetEnumerator();
while (iter.MoveNext())
{
Console.WriteLine("{0}", iter.Current);
}
}
public static void MyDisplayEventsInfo(Type theType)
{
EventDescriptorCollection eventDescriptors = TypeDescriptor.GetEvents(theType);
IEnumerator iter = eventDescriptors.GetEnumerator();
while (iter.MoveNext())
{
EventDescriptor cur = (EventDescriptor)iter.Current;
Console.WriteLine("{0}", cur.Name);
Console.WriteLine("{0}", cur.EventType);
Console.WriteLine("{0}", cur.Description);
Console.WriteLine("{0}", cur.Category);
Console.WriteLine("{0}", cur.DesignTimeOnly);
Console.WriteLine("{0}", cur.IsBrowsable);
Console.WriteLine("{0}", cur.IsMulticast);
}
}
public static void MyDisplayPropertiesInfo(Type theType)
{
PropertyDescriptorCollection propDescriptors = TypeDescriptor.GetProperties(theType);
IEnumerator iter = propDescriptors.GetEnumerator();
while (iter.MoveNext())
{
PropertyDescriptor cur = (PropertyDescriptor)iter.Current;
Console.WriteLine("{0}", cur.Name);
Console.WriteLine("{0}", cur.PropertyType);
Console.WriteLine("{0}", cur.Description);
Console.WriteLine("{0}", cur.Category);
Console.WriteLine("{0}", cur.DesignTimeOnly);
Console.WriteLine("{0}", cur.IsBrowsable);
Console.WriteLine("{0}", cur.IsReadOnly);
Console.WriteLine();
}
}
{
Console.WriteLine(theType.FullName);
EventDescriptor defaultEvent = TypeDescriptor.GetDefaultEvent(theType);
if (defaultEvent != null)
{
Console.WriteLine(defaultEvent.Name);
}
PropertyDescriptor defaultProperty = TypeDescriptor.GetDefaultProperty(theType);
if (defaultProperty != null)
{
Console.WriteLine(defaultProperty.Name);
}
Console.WriteLine("Attributes:");
AttributeCollection attribs = TypeDescriptor.GetAttributes(theType);
IEnumerator iter = attribs.GetEnumerator();
while (iter.MoveNext())
{
Console.WriteLine("{0}", iter.Current);
}
}
public static void MyDisplayEventsInfo(Type theType)
{
EventDescriptorCollection eventDescriptors = TypeDescriptor.GetEvents(theType);
IEnumerator iter = eventDescriptors.GetEnumerator();
while (iter.MoveNext())
{
EventDescriptor cur = (EventDescriptor)iter.Current;
Console.WriteLine("{0}", cur.Name);
Console.WriteLine("{0}", cur.EventType);
Console.WriteLine("{0}", cur.Description);
Console.WriteLine("{0}", cur.Category);
Console.WriteLine("{0}", cur.DesignTimeOnly);
Console.WriteLine("{0}", cur.IsBrowsable);
Console.WriteLine("{0}", cur.IsMulticast);
}
}
public static void MyDisplayPropertiesInfo(Type theType)
{
PropertyDescriptorCollection propDescriptors = TypeDescriptor.GetProperties(theType);
IEnumerator iter = propDescriptors.GetEnumerator();
while (iter.MoveNext())
{
PropertyDescriptor cur = (PropertyDescriptor)iter.Current;
Console.WriteLine("{0}", cur.Name);
Console.WriteLine("{0}", cur.PropertyType);
Console.WriteLine("{0}", cur.Description);
Console.WriteLine("{0}", cur.Category);
Console.WriteLine("{0}", cur.DesignTimeOnly);
Console.WriteLine("{0}", cur.IsBrowsable);
Console.WriteLine("{0}", cur.IsReadOnly);
Console.WriteLine();
}
}
3.利用元数据,可以实现VS2005的属性管理器
这里要注意两个方法:TypeConverter,负责将值转换成其它数据类型,反之亦然
TypeDescriptor.GEtEditor()方法,需要参数:属性的数据类型,编辑器的基本数据类型(UITypeEditor是所有编辑器类的基类)。
4.组件类是一个实现IComponent接口的简单类。
约束:组件类必须提供一个无参ctor,或者是一个带IContainer类型参数的ctor。
实现IComponent接口的几种方式:直接/间接继承于以下类
1.System.ComponentModel.Component,适用于引用编组对象
2.System.ComponentModel.MarshalByValueComponent,适用于值编组
3.System.Web.UI.Control 和 System.Windows.Forms.Control
4.System.Web.HttpApplication
5.自定义容器
首先研究Form的默认生成代码:
ISite接口和IContainer接口
6.一些元数据:[Browseable()],[Category()]等等
其中,[ParenthesizePropertyName()]表示属性名应该在圆括号对内
[RefreshProperties(RefreshProperties.All)]表示该属性改变,会刷新全部属性
编辑器类DateTimeEditor,允许在属性窗口以图形形式编辑DateTime的值,这是默认的,不需要在Datetime类型的字段显示标记
同理,有ArrayEditor和ColorEditor
7.转换器类 派生于TypeConverter
组件编程不止这些,我准备去研究《ASP.NET服务器组件编程》那本书,以求更大突破。