Definition: An attribute is a piece of additional declarative information that is specified for a declaration (MSDN).
So, we can look Attribute like a Decorator from the definition.
Example:
[Obsolete("This method is substituted by xxx", true)]
public void Demo() { ... }
param1: 为什么该元素被荒弃,以及我们该使用什么元素来代替它.
param2: 告诉编译器把依然使用这被标识的元素视为一种错误,这就意味着编译器会因此而产生一个警告.
Develop Customizd Attribute:
A class that derives from the abstract class System.Attribute, whether directly or indirectly, is an attribute class. The declaration of an attribute class defines a new kind of attribute that can be placed on a declaration (MSDN).
必选参数和命名参数
必选参数:在Contructor中进行初始化
命名参数:未在Contructor中初始化,但定义了Set方法的Property.
参数类型:simple data type, System.Type, object
public class MyAttribute : Attribute { protected string description; protected string version; public MyAttribute(string description) { this.description = description; } public string Description { get { return this.description;} set { this.description = value;} } public string Version { get { return this.version;} set { this.version = value;} } }
使用:
[MyAttribute("This is a class", Version = "1.0", Description = "This is a class 2")] public class TestClass { ... }
此时
MyAttribute.Description : This is a class 2 //先在构造时被设为This is a class, 后又被设为 ... 2
MyAttribute.Version : 2.0
约束自定义Attribute的使用
AttributeUsage 类: 描述了一个自定义attribute类能被怎样使用. attribute类本身用这个atrribute System.AttributeUsage来标记.
AttributeUsage的三个属性:
1. ValidOn: 指定约束的attribute可以应用在哪些语言元素上. 可选的值有:AttributeTargets. {Assembly, Moudle, Class, Struct, Enum, Constructor, Method, Property, Field, Event, Interface, Parameter, Delegate, All, ClassMembers = (Class | Struct | Enum | Constructor | Method | Property | Field | Event | Delegate | Interface)}. default: AttributeTargets.All
2. AllowMultiple: 指示约束的attribute是否可在同一语言元素上使用多次. default: false
3. Inherited: 指示约束的attribute是否被派生类继承. default: false
示例:
1. Attribute Definition
using system; [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false )] public class MyAttribute : Attribute { public MyAttribute(string description){...} ... }
2. Using
[MyAttribute("This is MyClass")] [MyAttribute("This is MyClass")] //Error: because AllowMultiple = false public class MyClass { [MyAttribute("This is a Method")] //Error: AttributeTarges.Class specified that we just can use this attribute on a class public void Show() {...} }
Attribute标记
功能: 约束将Attribute应用到哪一个语言元素
可用标识符: assembly, moudle, type, method, property, event, field, param, return
功能示例:
1. Attribute 放在哪儿才能让编译器确定该attribute是绑定至整个assembly?
2. 怎样才能让编译器确定我们是把attribute绑定至方法的返回类型上,而不是整个方法?
[assembly: MyAttribute(...)]
运行时查询Attributes (By Reflection)
1. 查询程序集的custom attribute
class TestApp { public static void Main() { Process p = Process.GetCurrentProcess(); string assemblyName = p.ProcessName + ".exe"; Assembly a = Assembly.LoadFrom(assemblyName); foreach (Attribute attr in a.GetCustomAttributes(true)) { if(typeof(MyAttribute).IsAssignableFrom(typeof(attr))) { Console.Write("Description of {0}: {1}", assemblyName, attr.Description); } } } }
2. 查询class, method, property的attributes
a) class: type.GetCustomAttributes(true) 得到所有绑定的attributes -> ...
b) method type.GetMethods() -> method.GetCustomAttributes(true) -> ...
c) property type.GetFields() -> field.GetCustomAttributes(true) -> ...
典型Attributes应用(待整理)
额外阅读推荐: