AttributeUsage属性

  除了定制 attributes 之外,可以使用 Attributes 属性定义如何使用这些属性。例如:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

[AttributeUsage( 
    validon, 
    AllowMultiple = allowmultiple, 
    Inherited = inherited
)] 

强烈推荐使用AttributeUsage属性将属性文档化,因此属性的用户能直接使用已命名的属性,而不用在源代码中查找公用的读/写字段和属性。

定义属性目标

 1 None.gif public   enum  AttributeTargets
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gif    Assembly    = 0x0001,
 4InBlock.gif    Module      = 0x0002,
 5InBlock.gif    Class       = 0x0004,
 6InBlock.gif    Struct      = 0x0008,
 7InBlock.gif    Enum        = 0x0010,
 8InBlock.gif    Constructor = 0x0020,
 9InBlock.gif    Method      = 0x0040,
10InBlock.gif    Property    = 0x0080,
11InBlock.gif    Field       = 0x0100,
12InBlock.gif    Event       = 0x0200,
13InBlock.gif    Interface   = 0x0400,
14InBlock.gif    Parameter   = 0x0800,
15InBlock.gif    Delegate    = 0x1000,
16InBlock.gif    All = Assembly │ Module │ Class │ Struct │ Enum │ Constructor │ 
17InBlock.gif        Method │ Property │ Field │ Event │ Interface │ Parameter │ 
18InBlock.gif        Delegate,
19InBlock.gif
20InBlock.gif    ClassMembers  =  Class │ Struct │ Enum │ Constructor │ Method │ 
21InBlock.gif        Property │ Field │ Event │ Delegate │ Interface,
22ExpandedBlockEnd.gif}

23 None.gif

当使用 Attribute 属性时,能指定 AttributeTargets.all (属性目标),因此属性能被附加到在枚举 AttributeTargets 列出的任意类型上。若未指定 AttributeUsage 属性,缺省值是 AttributeTargets.All 。属性 AttributeTargets 用来限制属性使用范围。

 1 None.gif [AttributeUsage(AttributeTargets.Class)]
 2 None.gif public   class  RemoteObjectAttribute : Attribute
 3 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 4InBlock.gif
 5ExpandedBlockEnd.gif}

 6 None.gif
 7 None.gif[AttributeUsage(AttributeTargets.Method)]
 8 None.gif public   class  TransactionableAttribute : Attribute
 9 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
10InBlock.gif
11InBlock.gif
12InBlock.gif
13ExpandedBlockEnd.gif}

14 None.gif

  可以使用或( | )操作符组合属性目标枚举中列出的项。

 单一用途和多用途属性

可以使用AttributeUsage定义属性的单一用途或多用途。即确定在单个字段上使用单一属性的次数。在缺省情况下,所有属性都是单用途的。在AttributeUsage属性中,指定AllowMultipletrue,则允许属性多次附加到指定的类型上。例如:

 1 None.gif [AttributeUsage(AttributeTargets.All, AllowMultiple = true )]
 2 None.gif public   class  SomethingAttribute : Attribute
 3 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 4InBlock.gif    public SomethingAttribute(String str)
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 6ExpandedSubBlockEnd.gif    }

 7ExpandedBlockEnd.gif}

 8 None.gif
 9 None.gif[Something( " abc " )]
10 None.gif[Something( " def " )]
11 None.gif class  MyClass
12 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
13ExpandedBlockEnd.gif}

14 None.gif

 

指定继承属性规则

   AttributeUsageAttribute属性的最后部分是继承标志,用于指定属性是否能被继承。缺省值是false。然而,若继承标志被设置为true,它的含义将依赖于AllowMultiple标志的值。若继承标志被设置为true,并且AllowMultiple标志是flag,则改属性将忽略继承属性。若继承标志和AllowMultiple标志都被设置为true,则改属性的成员将与派生属性的成员合并。范例:

 1 None.gif using  System;
 2 None.gif using  System.Reflection;
 3 None.gif
 4 None.gif namespace  AttribInheritance
 5 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 6InBlock.gif    [AttributeUsage(
 7InBlock.gif        AttributeTargets.All, 
 8InBlock.gif        AllowMultiple = true,
 9InBlock.gif        //AllowMultiple = false,
10InBlock.gif        Inherited = true
11InBlock.gif    )]
12InBlock.gif    public class SomethingAttribute : Attribute
13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
14InBlock.gif        private string name;
15InBlock.gif        public string Name
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
17ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn name; }
18ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ name = value; }
19ExpandedSubBlockEnd.gif        }

20InBlock.gif
21InBlock.gif        public SomethingAttribute(string str)
22ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
23InBlock.gif            this.name = str;
24ExpandedSubBlockEnd.gif        }

25ExpandedSubBlockEnd.gif    }

26InBlock.gif
27InBlock.gif     [Something("abc")]
28InBlock.gif    class MyClass
29ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
30ExpandedSubBlockEnd.gif    }

31InBlock.gif
32InBlock.gif    [Something("def")]
33InBlock.gif    class Another : MyClass
34ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
35ExpandedSubBlockEnd.gif    }

36InBlock.gif
37InBlock.gif    class Test
38ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
39InBlock.gif        [STAThread]
40InBlock.gif        static void Main(string[] args)
41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
42InBlock.gif            Type type =
43InBlock.gif                Type.GetType("AttribInheritance.Another");
44InBlock.gif            foreach (Attribute attr in type.GetCustomAttributes(true))
45InBlock.gif                //type.GetCustomAttributes(false))
46ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
47InBlock.gif                SomethingAttribute sa = 
48InBlock.gif                    attr as SomethingAttribute;
49InBlock.gif                if (null != sa)
50ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
51InBlock.gif                    Console.WriteLine(
52InBlock.gif                        "Custom Attribute: {0}"
53InBlock.gif                        sa.Name);
54ExpandedSubBlockEnd.gif                }

55ExpandedSubBlockEnd.gif            }

56InBlock.gif
57ExpandedSubBlockEnd.gif        }

58ExpandedSubBlockEnd.gif    }

59ExpandedBlockEnd.gif}

60 None.gif

 

AllowMultiple设置为false,结果是:

Custom Attribute: def

 AllowMultiple设置为true,结果是:

Custom Attribute: def

Custom Attribute: abc

转载于:https://www.cnblogs.com/stzyw/archive/2005/10/07/249693.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值