导航

原文:http://msdn.microsoft.com/zh-cn/library/z919e8tw.aspx

 

如果没有检索自定义特性的信息和对其进行操作的方法,则定义自定义特性并将其放置在源代码中就没有意义。 使用反射,可检索用自定义特性定义的信息。 主要方法是 GetCustomAttributes,它返回对象数组,这些对象在运行时等效于源代码特性。 此方法具有多个重载版本。 有关更多信息,请参见 Attribute

特性规范,如:

[Author("P. Ackerman", version = 1.1)]
class SampleClass

在概念上等效于:

Author anonymousAuthorObject = new Author("P. Ackerman");
anonymousAuthorObject.version = 1.1;

但是,直到查询 SampleClass 来获取特性后才会执行此代码。 SampleClass 调用 GetCustomAttributes 会导致按上述方式构造并初始化一个 Author 对象。 如果该类具有其他特性,则按相似的方式构造其他特性对象。 然后 GetCustomAttributes 返回 Author 对象和数组中的任何其他特性对象。 之后就可以对此数组进行迭代,确定根据每个数组元素的类型所应用的特性,并从特性对象中提取信息。



 

示例:定义一个自定义特性,将其应用于若干实体并通过反射进行检索。

 1 // Multiuse attribute.
2 [System.AttributeUsage(System.AttributeTargets.Class |
3 System.AttributeTargets.Struct,
4 AllowMultiple = true) // Multiuse attribute.
5 ]
6 public class Author : System.Attribute
7 {
8 string name;
9 public double version;
10
11 public Author(string name)
12 {
13 this.name = name;
14
15 // Default value.
16 version = 1.0;
17 }
18
19 public string GetName()
20 {
21 return name;
22 }
23 }
24
25 // Class with the Author attribute.
26 [Author("P. Ackerman")]
27 public class FirstClass
28 {
29 // ...
30 }
31
32 // Class without the Author attribute.
33 public class SecondClass
34 {
35 // ...
36 }
37
38 // Class with multiple Author attributes.
39 [Author("P. Ackerman"), Author("R. Koch", version = 2.0)]
40 public class ThirdClass
41 {
42 // ...
43 }
44
45 class TestAuthorAttribute
46 {
47 static void Test()
48 {
49 PrintAuthorInfo(typeof(FirstClass));
50 PrintAuthorInfo(typeof(SecondClass));
51 PrintAuthorInfo(typeof(ThirdClass));
52 }
53
54 private static void PrintAuthorInfo(System.Type t)
55 {
56 System.Console.WriteLine("Author information for {0}", t);
57
58 // Using reflection.
59 System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t); // Reflection.此处用的是System.Attribute.GetCustomAttributes(MemberInfo)的重载,Type是继承于MemberInfo的类
60
61 // Displaying output.
62 foreach (System.Attribute attr in attrs)
63 {
64 if (attr is Author)
65 {
66 Author a = (Author)attr;
67                 System.Console.WriteLine(" {0}, version {1:f}", a.GetName(), a.version);
68 }
69 }
70 }
71 }
72 /* Output:
73 Author information for FirstClass
74 P. Ackerman, version 1.00
75 Author information for SecondClass
76 Author information for ThirdClass
77 R. Koch, version 2.00
78 P. Ackerman, version 1.00
79 */


注意第66、67行:

如果省略66这步,访问时直接写成(Author)attr.GetName()是无效的,由于运算符优先的问题,必须写成((Author)attr).GetName()