AttributeUsage属性
除了定制attributes之外,可以使用Attributes属性定义如何使用这些属性。例如:
当使用Attribute属性时,能指定AttributeTargets.all(属性目标),因此属性能被附加到在枚举AttributeTargets列出的任意类型上。若未指定AttributeUsage属性,缺省值是AttributeTargets.All。属性AttributeTargets用来限制属性使用范围。
可以使用或(|)操作符组合属性目标枚举中列出的项。
[AttributeUsage(
validon,
AllowMultiple = allowmultiple,
Inherited = inherited
)]
强烈推荐使用AttributeUsage属性将属性文档化,因此属性的用户能直接使用已命名的属性,而不用在源代码中查找公用的读/写字段和属性。
定义属性目标
1
public enum AttributeTargets
2
{
3
Assembly = 0x0001,
4
Module = 0x0002,
5
Class = 0x0004,
6
Struct = 0x0008,
7
Enum = 0x0010,
8
Constructor = 0x0020,
9
Method = 0x0040,
10
Property = 0x0080,
11
Field = 0x0100,
12
Event = 0x0200,
13
Interface = 0x0400,
14
Parameter = 0x0800,
15
Delegate = 0x1000,
16
All = Assembly │ Module │ Class │ Struct │ Enum │ Constructor │
17
Method │ Property │ Field │ Event │ Interface │ Parameter │
18
Delegate,
19![](/Images/OutliningIndicators/InBlock.gif)
20
ClassMembers = Class │ Struct │ Enum │ Constructor │ Method │
21
Property │ Field │ Event │ Delegate │ Interface,
22
}
23![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
23
![](/Images/OutliningIndicators/None.gif)
当使用Attribute属性时,能指定AttributeTargets.all(属性目标),因此属性能被附加到在枚举AttributeTargets列出的任意类型上。若未指定AttributeUsage属性,缺省值是AttributeTargets.All。属性AttributeTargets用来限制属性使用范围。
1
[AttributeUsage(AttributeTargets.Class)]
2
public class RemoteObjectAttribute : Attribute
3
{
4
…
5
}
6![](/Images/OutliningIndicators/None.gif)
7
[AttributeUsage(AttributeTargets.Method)]
8
public class TransactionableAttribute : Attribute
9
{
10![](/Images/OutliningIndicators/InBlock.gif)
11![](/Images/OutliningIndicators/InBlock.gif)
12![](/Images/OutliningIndicators/InBlock.gif)
13
}
14![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
14
![](/Images/OutliningIndicators/None.gif)
可以使用或(|)操作符组合属性目标枚举中列出的项。
单一用途和多用途属性
可以使用AttributeUsage定义属性的单一用途或多用途。即确定在单个字段上使用单一属性的次数。在缺省情况下,所有属性都是单用途的。在AttributeUsage属性中,指定AllowMultiple为true,则允许属性多次附加到指定的类型上。例如:
1
[AttributeUsage(AttributeTargets.All, AllowMultiple=true)]
2
public class SomethingAttribute : Attribute
3
{
4
public SomethingAttribute(String str)
5
{
6
}
7
}
8![](/Images/OutliningIndicators/None.gif)
9
[Something("abc")]
10
[Something("def")]
11
class MyClass
12
{
13
}
14![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
7
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
13
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
14
![](/Images/OutliningIndicators/None.gif)
指定继承属性规则
在AttributeUsageAttribute属性的最后部分是继承标志,用于指定属性是否能被继承。缺省值是false。然而,若继承标志被设置为true,它的含义将依赖于AllowMultiple标志的值。若继承标志被设置为true,并且AllowMultiple标志是flag,则改属性将忽略继承属性。若继承标志和AllowMultiple标志都被设置为true,则改属性的成员将与派生属性的成员合并。范例:
1
using System;
2
using System.Reflection;
3![](/Images/OutliningIndicators/None.gif)
4
namespace AttribInheritance
5
{
6
[AttributeUsage(
7
AttributeTargets.All,
8
AllowMultiple = true,
9
//AllowMultiple = false,
10
Inherited = true
11
)]
12
public class SomethingAttribute : Attribute
13
{
14
private string name;
15
public string Name
16
{
17
get { return name; }
18
set { name = value; }
19
}
20![](/Images/OutliningIndicators/InBlock.gif)
21
public SomethingAttribute(string str)
22
{
23
this.name = str;
24
}
25
}
26![](/Images/OutliningIndicators/InBlock.gif)
27
[Something("abc")]
28
class MyClass
29
{
30
}
31![](/Images/OutliningIndicators/InBlock.gif)
32
[Something("def")]
33
class Another : MyClass
34
{
35
}
36![](/Images/OutliningIndicators/InBlock.gif)
37
class Test
38
{
39
[STAThread]
40
static void Main(string[] args)
41
{
42
Type type =
43
Type.GetType("AttribInheritance.Another");
44
foreach (Attribute attr in type.GetCustomAttributes(true))
45
//type.GetCustomAttributes(false))
46
{
47
SomethingAttribute sa =
48
attr as SomethingAttribute;
49
if (null != sa)
50
{
51
Console.WriteLine(
52
"Custom Attribute: {0}",
53
sa.Name);
54
}
55
}
56![](/Images/OutliningIndicators/InBlock.gif)
57
}
58
}
59
}
60![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
55
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
58
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
59
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
60
![](/Images/OutliningIndicators/None.gif)
若AllowMultiple设置为false,结果是:
Custom Attribute: def
若AllowMultiple设置为true,结果是:
Custom Attribute: def
Custom Attribute: abc