AttributeUsage

本文整理自网络。

         首先你得理解什么是Attribute(这里翻译为特性,属性应该对应于(Property),是c#另一个内容了),特性允许我们添加类似关键字(比如public)的批注来为元素(可以是class,method,property,parameter,event等),在运行时可以通过反射获得这些信息,从而影响程序的行为。

        对于Attribute,我们可以使用AttributesUage施加在这些特性(attribute)之上。我们姑且可以称AttributeUsage为元特性(专门施加在特性之上的特性)

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

     例如:[AttributeUsage(AttributeTargets.Class, AllowMultiple = true,  Inherited = true)]//注意后面两个参数必须以具名参数的方式给出,不能直接写true,false。


      下面分别讲解这三个参数:


一、validon

定义特性目标

 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
20     ClassMembers  =  Class │ Struct │ Enum │ Constructor │ Method │ 
21         Property │ Field │ Event │ Delegate │ Interface,
22 }
23

       当使用Attribute特性时,我们有时需要指定这些特性施加的目标类型AttributeTargets。我们给定AttributeTargets.Class,因此特性只能被附加到类类型上。若未指定AttributeUsage特性,缺省值是AttributeTargets.All。特性AttributeTargets用来限制特性使用范围。


 1 [AttributeUsage(AttributeTargets.Class)]
 2 public  class RemoteObjectAttribute : Attribute
 3 {
 4
 5 }
 6
 7 [AttributeUsage(AttributeTargets.Method)]
 8 public  class TransactionableAttribute : Attribute
 9 {
10
11
12
13 }
14

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

 单一用途和多用途特性

        可以使用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
 9     [Something("abc")]
10 [Something("def")]
11 class MyClass
12 {
13 }
14

 

指定继承特性规则

      在AttributeUsageAttribute特性的最后部分是继承标志,指示父类的某个特性是否能被子类继承。缺省值是false。当该参数为FALSE时,一切都很容易理解。然而,若继承标志被设置为true,它的含义将依赖于AllowMultiple标志的值。(原文这里讲的并不清楚)

      若继承标志被设置为true,AllowMultiple标志是false,则子类该类型特性将覆盖父类该类型特性。

     若继承标志被设置为true,AllowMultiple标志是true,则父类该特性的成员与子类同类型特性的成员同时存在于子类。

范例:

 1 using System;
 2 using System.Reflection;
 3
 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
21          public SomethingAttribute( string str)
22          {
23              this.name = str;
24         }
25     }
26
27      [Something("MyClass")]
28      class MyClass
29      {
30     }
31
32     [Something("Another")]
33      class Another : MyClass
34      {
35     }
36
37      class Test
38      {
39         [STAThread]
40          static  void Main( string[] args)
41          {
42             Type type =   Type.GetType("AttribInheritance.Another");//获取Another的类型信息
44              foreach (Attribute attr  in type.GetCustomAttributes( true))//获取自定义特性,true表示也获取继承的特性。
46              {
47                 SomethingAttribute sa =    attr  as SomethingAttribute;//类型转换
49                  if ( null != sa)
50                  {
51                      Console.WriteLine(sa.Name);
54                 }
55             }
56
57         }
58     }
59 }
60

若AllowMultiple设置为false,结果是:

Another

 若AllowMultiple设置为true,结果是:

MyClass

Another

原文地址:

http://www.cnblogs.com/stzyw/archive/2005/10/07/249693.html

参考文献:

《C# 4.0权威指南》

http://bbs.csdn.net/topics/360111279

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值