特性类
从抽象类System.Attribute派生的类。可附加到各种程序实体,而且在运行时环境中可以检索这些特性信息。
1. AttributeUsage类
施加在自定义特性类上的特性,控制自定义特性的使用。
[AttributeUsage(System.AttributeTargets, AllowMultiple, Inherited)]
(1) System.AttributeTargets:在哪些元素上使用,可将枚举进行“|”运算。
标记 | 用途 |
All | 可以对任何应用程序元素应用 |
Assembly | 对程序集应用 |
Module | 对模块应用 |
Class | 对类应用 |
Struct | 对结构应用 |
Enum | 枚举 |
Constructor | 构造函数 |
Method | 方法 |
Property | 属性 |
Field | 字段 |
Event | 事件 |
Interface | 接口 |
Parameter | 参数 |
Delegate | 委托 |
ReturnValue | 返回值 |
GenericParameter | 泛型参数 |
(2) AllowMultiple:bool类型,同一特性是否可多次施加在同一元素上。
(3) Inherited:施加自定义特性的元素在被继承时,子类是否能继承元素上的自定义特性。
2. 自定义特性
3. 使用自定义特性
4. 取出信息
5. 运行结果