中文翻译称特性 和 类的Property属性 区别
Atrribute何用?
首先,我们肯定Attribute是一个类,下面是msdn文档对它的描述:
公共语言运行时允许你添加类似关键字的描述声明,叫做 attributes, 它对程序中的元素进行标注,如类型、字段、方法和属性等。Attributes和Microsoft .NET Framework文件的元数据保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为。
在.NET中,Attribute被用来处理多种问题,比如序列化、程序的安全特征、防止即时编译器对程序代码进行优化从而代码容易调试等等。下 面,我们先来看几个在.NET中标准的属性的使用,稍后我们再回过头来讨论Attribute这个类本身。(文中的代码使用C#编写,但同样适用所有基 于.NET的所有语言)
AttributeUsage()
用户自己定义的Attribute需要实现 这个特性
AttributeUsage(AtrtributTargets, AllowMultiple, Inherited);
AttributeUsage本身也是一个Attribute,这是专门施加在Attribute类的Attribute. AttributeUsage自然也是从Attribute派生,它有一个带参数的构造器,这个参数是AttributeTargets的枚举类型。下面 是AttributeTargets 的定义:

Code
public enum AttributeTargets
{
All=16383,
Assembly=1,
Module=2,
Class=4,
Struct=8,
Enum=16,
Constructor=32,
Method=64,
Property=128,
Field=256,
Event=512,
Interface=1024,
Parameter=2048,
Delegate=4096,
ReturnValue=8192
}
AllowMultiple: 读取或者设置这个属性,表示是否可以对一个程序元素施加多个Attribute 。
Inherited:读取或者设置这个属性,表示是否施加的Attribute 可以被派生类继承或者重载。
ValidOn: 读取或者设置这个属性,指明Attribute 可以被施加的元素的类型。
如

Code
1 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct,
2
3 Inherited = true,
4
5 AllowMultiplye = true)]
6 public class ErrorCode
7 {
8 //这个类记录调试时某段代码的错误信息
9 }
10
介绍了这么多 下面自己先写一个类 该类用于调试时记录发现代码的时间和作者 以及错误原因等。

Code
1 using System;
2 using System.Text;
3
4 namespace MyA
5 {
6 [AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = true)]
7 public class ErrorAttribute : System.Attribute
8 {
9 public ErrorAttribute(string date, string author, string errormessage)
10 {
11 this.Date = date;
12 this.Author = author;
13 this.ErrorMessage = errormessage;
14 }
15
16 public string Date
17 {
18 get { return date; }
19 set { date = value; }
20 }
21
22 public string Author
23 {
24 get { return author; }
25 set { author = value; }
26 }
27
28 public string ErrorMessage
29 {
30 get { return errormessage; }
31 set { errormessage = value; }
32 }
33 private string date;
34 private string author;
35 private string errormessage;
36 }
37 }
一个测试类 测试ErrorAttribute 在class和 method上放入多个attribute

Code
1 [MyA.Error("2008-2-2", "Huang", "Fuckl Error1")]
2 [MyA.Error("2008-2-2", "Huang", "Fuckl Error5")]
3 public class SomeClass
4 {
5 [MyA.Error("2008-2-2", "Huang", "Fuckl Error2")]
6 public SomeClass()
7 {
8 }
9
10 [MyA.Error("2008-2-2", "Huang", "Fuckl Error3")]
11 public void TestMethod1()
12 {
13 }
14
15 [MyA.Error("2008-2-2", "Huang", "Fuckl Error4")]
16 public void TestMethod2()
17 {
18 }
19 }
20
在下面的代码中获取该测试类的不同范围的属性信息。

Code
class Program
{
static void Main(string[] args)
{
MyA.ErrorAttribute [] errorA;
errorA = (ErrorAttribute [] )(Attribute.GetCustomAttributes(typeof(SomeClass), typeof(ErrorAttribute)));
//获取类中的attribute
foreach (ErrorAttribute e in errorA)
{
Console.WriteLine("Time:{0}", e.Date);
Console.WriteLine("Author:{0}", e.Author);
Console.WriteLine("Error:{0}", e.ErrorMessage);
}
Type type = typeof(SomeClass);
MethodInfo [] method = type.GetMethods();
ErrorAttribute errorB;
//获取方法中的attribute
foreach (MethodInfo m in method)
{
errorB = (ErrorAttribute)(Attribute.GetCustomAttribute(m, typeof(ErrorAttribute)));
if (errorB != null)
{
Console.WriteLine("Time:{0}", errorB.Date);
Console.WriteLine("Author:{0}", errorB.Author);
Console.WriteLine("Error:{0}", errorB.ErrorMessage);
}
}
Console.ReadLine();
}
}
特性在声明时没有被初始化 只是在类型反射时才初始化。