元数据attribute主要分为两大类:
- 设计期attribute
- 解析期attribute
设计期attribute
设计期attribute对于控件在可视化设计器(如.NET 2003)内正确运行是很重要的,设计器、属性浏览器和其他的设计期元素使用设计期attribute提供的元数据的作用如下:
- 显示属性和事件
- 执行设计期的序列化
- 把用来实现设计期功能的类和控件或者属性类型关联起来
因为设计时特性向可视设计工具提供有价值的信息,所以它们对在设计时正确显示控件及其成员非常重要。
在下面的代码片段中,CategoryAttribute特性启用属性浏览器以在“Alignment”类别中显示 TextAlignment属性。DescriptionAttribute特性允许属性浏览器在用户单击某个属性时显示该属性的简要说明。
1 [C#]
2 [
3 Category("Alignment"),
4 Description("Specifies the alignment of text.")
5 ]
6 public ContentAlignment TextAlignment {
}
在 C# 和 Visual Basic .NET 中,名为 AttributeNameAttribute 的特性类在特性语法中可以被简单地引用为 AttributeName。
某些设计时特性是在类级别应用的。DesignerAttribute 特性在类级别应用,它通知窗体设计器使用哪个设计器类来显示控件。组件与默认的设计器 (System.ComponentModel.Design.ComponentDesigner) 关联,Windows 窗体和 ASP.NET 服务器控件与它们自己的默认设计器关联。只有在为组件或控件定义自定义设计器时才应用 DesignerAttribute。
1 [C#]
2 // Associates the designer class SimpleControl.Design.SimpleDesigner
3 // with Simple.
4 [ Designer(typeof(SimpleControl.Design.SimpleDesigner))]
5 public class Simple : WebControl { //
}
1.显示属性和事件的attribute
下面的attribute在设计期里用来显示属性和事件。
1)BrowsableAttribute 类
指定一个属性或事件是否应显示在“属性”窗口中。
System.Object
System.Attribute
System.ComponentModel.BrowsableAttribute
概要:告知属性浏览器,是否在属性浏览器中显示属性或事件。
应用:属性和事件
样例中的使用:[Browsable(true)]
参数类型:Boolean
默认值:true
注意:当检查代码中此特性的值时,必须将该特性指定为BrowsableAttribute.Yes 或BrowsableAttribute.No。
示例:下面的示例将属性标记为可浏览。
1 [C#]
2 [Browsable(true)]
3 public int MyProperty
4 {
5 get {
6 // Insert code here.
7 return 0;
8 }
9 set {
10 // Insert code here.
11 }
12 }
下一个示例显示如何检查 MyProperty 的 BrowsableAttribute 的值。
首先,代码获取具有该对象的所有属性的 PropertyDescriptorCollection。
接着,将代码编入 PropertyDescriptorCollection 的索引,以获取 MyProperty。
然后它返回该属性的特性,并将它们保存到特性变量中。
该示例提供两种不同的方法检查 BrowsableAttribute 的值。
在第二个代码片断中,示例调用 Equals 方法。在最后一个代码段中,该示例使用 Browsable 属性检查此值。
1 [C#]
2 // Gets the attributes for the property.
3 AttributeCollection attributes =
4 TypeDescriptor.GetProperties(this)["MyProperty"].Attributes;
5
6 // Checks to see if the value of the BrowsableAttribute is Yes.
7 if(attributes[typeof(BrowsableAttribute)].Equals(BrowsableAttribute.Yes)) {
8 // Insert code here.
9 }
10
11 // This is another way to see whether the property is browsable.
12 BrowsableAttribute myAttribute =
13 (BrowsableAttribute)attributes[typeof(BrowsableAttribute)];
14 if(myAttribute.Browsable) {
15 // Insert code here.
16 }
17
如果将类标记为 BrowsableAttribute,可使用下列代码检查该值。
1 [C#]
2 AttributeCollection attributes =
3 TypeDescriptor.GetAttributes(MyProperty);
4 if(attributes[typeof(BrowsableAttribute)].Equals(BrowsableAttribute.Yes)) {
5 // Insert code here.
6 }
7
2)BindableAttribute 类
指定属性是否通常用于绑定。
System.Object
System.Attribute
System.ComponentModel.BindableAttribute
概要:告知属性浏览器绑定数据到属性是否有意义。
带有Bindable[true]标记的属性能显示在从属性浏览器中装载的DataBinding对话框里,
页面开发者利用它可以把属性和数据绑定表达式关联起来。
应用:只应用于属性
样例中的使用:[Bindable(true)]
参数类型:Boolean
默认值:false
注意:若要在代码中检查该特性的值,必须将该特性指定为 BindableAttribute.Yes 或 BindableAttribute.No。
只能在设计时使用该特性。在运行时可以随意向任何属性进行绑定。
如果属性没有标记为Bindable(true),页面开发者仍能够通过在.aspx页面里手动输入表达式
来把属性和一个数据绑定表达式关联起来。
示例:下面的示例将属性标记为适于将数据绑定到的属性。
1 [C#]
2 [Bindable(true)]
3 public int MyProperty {
4 get {
5 // Insert code here.
6 return 0;
7 }
8 set {
9 // Insert code here.
10 }
11 }
下一个示例显示如何检查 MyProperty 的 BindableAttribute 的值。
首先,代码获取具有该对象的所有属性的 PropertyDescriptorCollection。
接着它使用索引进入 PropertyDescriptorCollection 以获取 MyProperty。
最后,它返回该属性的特性,并将它们保存在特性变量中。
此示例介绍两种不同的检查 BindableAttribute 的值的方法。
在第二个代码片断中,示例调用 Equals 方法。
在最后一个代码片段中,该示例使用 Bindable 属性检查该值。
1 [C#]
2 // Gets the attributes for the property.
3 AttributeCollection attributes =
4 TypeDescriptor.GetProperties(this)["MyProperty"].Attributes;
5
6 // Checks to see if the value of the BindableAttribute is Yes.
7 if(attributes[typeof(BindableAttribute)].Equals(BindableAttribute.Yes)) {
8 // Insert code here.
9 }
10
11 // This is another way to see whether the property is bindable.
12 BindableAttribute myAttribute =
13 (BindableAttribute)attributes[typeof(BindableAttribute)];
14 if(myAttribute.Bindable) {
15 // Insert code here.
16 }
17
18 // Yet another way to see whether the property is bindable.
19 if (attributes.Contains(BindableAttribute.Yes)) {
20 // Insert code here.
21 }
如果将类标记为 BindableAttribute,则可使用以下代码检查该值。
1 [C#]
2 AttributeCollection attributes =
3 TypeDescriptor.GetAttributes(MyProperty);
4 if(attributes[typeof(BindableAttribute)].Equals(BindableAttribute.Yes)) {
5 // Insert code here.
6 }
3)CategoryAttribute 类
指定其属性或事件将显示在可视化设计器中的类别。
System.Object
System.Attribute
System.ComponentModel.CategoryAttribute
概要:提供一个分类名,以这个分类名来显示属性或事件。
这个attribute允许属性浏览器以逻辑分组的形式显示属性和事件。
应用:属性和事件
样例中的使用:[Category("Appearance")]
参数类型:string
默认值:"Default"
注意:CategoryAttribute 指示在一个被设置为按分类顺序模式的
System.Windows.Forms.PropertyGrid 控件中列出属性或事件时,
将关联的属性或事件与之关联的类别。
如果没有对属性或事件应用 CategoryAttribute,
则 System.Windows.Forms.PropertyGrid 将属性或事件与杂项类别关联。
通过在 CategoryAttribute 的构造函数中指定类别的名称,
可以为任何名称创建新的类别。
Category 属性指示该属性所代表的类别的名称。
Category 属性还以透明方式对类别名称进行本地化。
这个attribute可以被局部化。
对继承者的说明:如果使用类别名称而不是预定义的名称,
并且想要本地化类别名称,
则必须重写 GetLocalizedString 方法。
此外,可以重写 Category 属性以提供您自己的本地化逻辑。
CategoryAttribute 类定义下列通用类别:
| 类别 |
说明 |
| Action |
与可用操作相关的属性。 |
| Appearance |
与实体的外观相关的属性。 |
| Behavior |
与实体的行为相关的属性。 |
| Data |
与数据和数据源管理相关的属性。 |
| Default |
组合到默认类别中的属性。 |
| Design |
仅在设计时可用的属性。 |
| DragDrop |
与拖放操作相关的属性。 |
| Focus |
与焦点相关的属性。 |
| Format |
与格式设置相关的属性。 |
| Key |
与键盘相关的属性。 |
| Layout |
与布局相关的属性。 |
| Mouse |
与鼠标相关的属性。 |
| WindowStyle |
与顶级窗体的窗口样式相关的属性。 |
示例:下面的示例创建 MyImage 属性。
1 [C#]
2 [Category("Appearance")]
3 public Image MyImage {
4 get {
5 // Insert code here.
6 return image1;
7 }
8 set {
9 // Insert code here.
10 }
11 }
下一个示例获取 MyImage 的类别。
首先,代码获取具有该对象的所有属性的 PropertyDescriptorCollection。
接着,将代码编入 PropertyDescriptorCollection 的索引,
以获取 MyImage。然后它返回该属性的特性,并将它们保存到 attributes 变量中。
然后,该示例通过以下方法输出类别:
从 AttributeCollection 检索 CategoryAttribute,然后将它写到控制台屏幕上。
1 [C#]
2 // Gets the attributes for the property.
3 AttributeCollection attributes =
4 TypeDescriptor.GetProperties(this)["MyImage"].Attributes;
5
6 // Prints the description by retrieving the CategoryAttribute.
7 // from the AttributeCollection.
8 CategoryAttribute myAttribute =
9 (CategoryAttribute)attributes[typeof(CategoryAttribute)];
10 Console.WriteLine(myAttribute.Category);
4)DefaultEventAttribute 类
指定组件的默认事件。
System.Object
System.Attribute
System.ComponentModel.DefaultEventAttribute
概要:告诉属性浏览器哪一个控件事件是默认事件。
允许页面开发者双击设计界面中的控件,为默认事件编写事件处理代码。
应用:只应用于事件
样例中的使用:[DefaultEvent("Click")]
参数类型:string
默认值:无
示例:以下示例定义名为 MyCollection 的集合类。
该类标记为 DefaultEventAttribute,它指定 CollectionChanged 作为默认事件。
1 [C#]
2 [DefaultEvent("CollectionChanged")]
3 public class MyCollection : BaseCollection {
4
5 private CollectionChangeEventHandler onCollectionChanged;
6
7 public event CollectionChangeEventHandler CollectionChanged {
8 add {
9 onCollectionChanged += value;
10 }
11 remove {
12 onCollectionChanged -= value;
13 }
14 }
15 // Insert additional code.
16 }
下一个示例创建 MyCollection 的实例。
然后它获取该类的特性,提取 DefaultEventAttribute,并输出默认事件的名称。
1 [C#]
2 public static int Main() {
3 // Creates a new collection.
4 MyCollection myNewCollection = new MyCollection();
5
6 // Gets the attributes for the collection.
7 AttributeCollection attributes = TypeDescriptor.GetAttributes(myNewCollection);
8
9 /* Prints the name of the default event by retrieving the
10 * DefaultEventAttribute from the AttributeCollection. */
11 DefaultEventAttribute myAttribute =
12 (DefaultEventAttribute)attributes[typeof(DefaultEventAttribute)];
13 Console.WriteLine("The default event is: " + myAttribute.Name);
14 return 0;
15 }
5)DefaultPropertyAttribute 类
指定组件的默认属性。
System.Object
System.Attribute
System.ComponentModel.DefaultPropertyAttribute
概要:告诉属性浏览器哪一个控件属性是默认属性。
页面开发者在设计界面中选择控件时,该属性被属性浏览器突出显示。
应用:只应用于属性
样例中的使用:[DefaultProperty("MyProperty")]
参数类型:string
默认值:无
示例:以下示例定义名为 MyControl 的控件。
该类标记为 DefaultPropertyAttribute,它指定 MyProperty 作为默认属性。
1 [C#]
2 [DefaultProperty("MyProperty")]
3 public class MyControl : Control {
4 public int MyProperty {
5 get {
6 // Insert code here.
7 return 0;
8 }
9 set {
10 // Insert code here.
11 }
12 }
13 // Insert any additional code.
14 }
下一个示例创建 MyControl 的实例。
然后它获取该类的特性,提取 DefaultPropertyAttribute,并输出默认属性的名称。
1 [C#]
2 public static int Main() {
3 // Creates a new control.
4 MyControl myNewControl = new MyControl();
5
6 // Gets the attributes for the collection.
7 AttributeCollection attributes = TypeDescriptor.GetAttributes(myNewControl);
8
9 /* Prints the name of the default property by retrieving the
10 * DefaultPropertyAttribute from the AttributeCollection. */
11 DefaultPropertyAttribute myAttribute =
12 (DefaultPropertyAttribute)attributes[typeof(DefaultPropertyAttribute)];
13 Console.WriteLine("The default property is: " + myAttribute.Name);
14
15 return 0;
16 }
6)DescriptionAttribute 类
指定属性或事件的说明。
System.Object
System.Attribute
System.ComponentModel.DescriptionAttribute
概要:提供一个简要的描述,在用户选择了属性或事件时,属性浏览器就显示这个描述。
应用:属性和事件
样例中的使用:[Description("The image associated with the control")]
参数类型:string
默认值:无
注意:可视化设计器在引用组件成员时可以显示指定的说明,
如在“属性”窗口中。调用 Description 访问该特性的值。
这个attribute可以被局部化。
示例:下面的示例创建 MyImage 属性。