2024年5月10日发(作者:)
AttributeUsage属性
AttributeUsage属性
除了定制attributes之外,可以使用Attributes属性定义如何使
用这些属性。例如:[AttributeUsage( validon,
AllowMultiple = allowmultiple,
Inherited = inherited
)]
强烈推荐使用AttributeUsage属性将属性文档化,因此属性的用
户能直接使用已命名的属性,而不用在源代码中查找公用的读/写字段
和属性。
定义属性目标
1public enum AttributeTargets
2{
3Assembly = 0x0001,
4Module = 0x0002,
5Class = 0x0004,
6Struct = 0x0008,
7Enum = 0x0010,
8Constructor = 0x0020,
9Method = 0x0040,
10Property = 0x0080,
11Field = 0x0100,
12Event = 0x0200,
13Interface = 0x0400,
14Parameter = 0x0800,
15Delegate = 0x1000,
16All = Assembly │ Module │ Class │ Struct │ Enum │
Constructor │
17Method │ Property │ Field │ Event │ Interface │
Parameter │
18Delegate,
19
20ClassMembers = Class │ Struct │ Enum │ Constructor │
Method │
21Property │ Field │ Event │ Delegate │ Interface,
22}
23
当使用Attribute属性时,能指定(属性目
标),因此属性能被附加到在枚举Attribute Targets列出的任意类型
上。若未指定AttributeUsage属性,缺省值是。
属性Attribut eTargets用来限制属性使用范围。
1[AttributeUsage()]
2public class RemoteObjectAttribute : Attribute
3{
4…
5}
6
7[AttributeUsage()]
8public class TransactionableAttribute : Attribute
9{
10
11
12
13}
14
可以使用或(|)操作符组合属性目标枚举中列出的项。单一用途
和多用途属性
可以使用AttributeUsage定义属性的单一用途或多用途。即确定
在单个字段上使用单一属性的次数。在缺省情况下,所有属性都是单
发布评论