• C# AttributeUsage的使用浅析


      C# AttributeUsage的使用是如何的呢?首先让我们来了解一下什么是AttributeUsage类它是另外一个预定义特性类,AttributeUsage类的作用就是帮助我们控制定制特性的使用。其实AttributeUsage类就是描述了一个定制特性如和被使用。

      C# AttributeUsage的使用要明白:

      AttributeUsage有三个属性,我们可以把它放置在定制属性前面。第一个属性是:

      ◆ValidOn 
      
      通过这个属性,我们能够定义定制特性应该在何种程序实体前放置。一个属性可以被放置的所有程序实体在AttributeTargets enumerator中列出。通过OR操作我们可以把若干个AttributeTargets值组合起来。

      ◆AllowMultiple 
      
     
      这个属性标记了我们的定制特性能否被重复放置在同一个程序实体前多次。

      ◆Inherited 
      
      我们可以使用这个属性来控制定制特性的继承规则。它标记了我们的特性能否被继承。

      C# AttributeUsage的使用实例:

      下面让我们来做一些实际的东西。我们将会在刚才的Help特性前放置AttributeUsage特性以期待在它的帮助下控制Help特性的使用。

        [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false )]
        public class HelpAttribute : Attribute
        {
            public HelpAttribute(string Description_in)
            {
                this._description = Description_in;
            }
    
            protected string _description;
    
            public string Description
            {
                get
                {
                    return this._description;
                }
            }
        }
    先让我们来看一下AttributeTargets.Class。它规定了Help特性只能被放在class的前面。这也就意味着下面的代码将会产生错误: 
        [Help("this is a do-nothing class")]
        public class AnyClass
        {
            [Help("this is a do-nothing method")] //error   
            public void AnyMethod()
            {
            }
        }

      编译器报告错误如下: 

    AnyClass.cs: Attribute 'Help' is not valid on this declaration type.  It is valid on 'class' declarations only.  

      我们可以使用AttributeTargets.All来允许Help特性被放置在任何程序实体前。可能的值是:

      Assembly,   
      Module,   
      Class,   
      Struct,   
      Enum,   
      Constructor,   
      Method,   
      Property,   
      Field,   
      Event,   
      Interface,   
      Parameter,   
      Delegate,   
      All = Assembly | Module | Class | Struct | Enum | Constructor |  Method | Property | Field | Event | Interface | Parameter | Delegate,   
      ClassMembers = Class | Struct | Enum | Constructor | Method | Property | Field | Event | Delegate | Interface 

      下面考虑一下AllowMultiple = false。它规定了特性不能被重复放置多次。

        [Help("this is a do-nothing class")]
        [Help("it contains a do-nothing method")]     //error
        public class AnyClass
        {
            public void AnyMethod()
            {
            }
        }

      它产生了一个编译期错误。

    AnyClass.cs: Duplicate 'Help' attribute 

    Ok,现在我们来讨论一下最后的这个属性。Inherited, 表明当特性被放置在一个基类上时,它能否被派生类所继承。

       [Help("BaseClass")]   
      public class Base   
      {   
      }   
         
      public class Derive : Base   
      {   
      }  

      C# AttributeUsage的使用会有四种可能的组合:

      [AttributeUsage(AttributeTargets.Class,  
       AllowMultiple = false, Inherited = false ]   
    
      [AttributeUsage(AttributeTargets.Class,   
      AllowMultiple = true, Inherited = false ]   
    
      [AttributeUsage(AttributeTargets.Class,   
      AllowMultiple = false, Inherited = true ]   
    
      [AttributeUsage(AttributeTargets.Class,   
      AllowMultiple = true, Inherited = true ] 
     

      C# AttributeUsage的使用第一种情况:

      如果我们查询(Query)Derive类,我们将会发现Help特性并不存在,因为inherited属性被设置为false。

                var type = typeof(Derive);
                Assert.False(type.IsDefined(typeof(HelpAttribute),true));
                Assert.False(type.IsDefined(typeof(HelpAttribute), false));

      C# AttributeUsage的使用第二种情况:

      和第一种情况相同,因为inherited也被设置为false。

      C# AttributeUsage的使用第三种情况:

      对于第三种和第四种,通过类型查找都是一样的

            [Fact]
            public void AttributeTest()
            {
                var type = typeof(Derive);
                Assert.True(type.IsDefined(typeof(HelpAttribute),true));
                Assert.False(type.IsDefined(typeof(HelpAttribute), false));
            }

      我们给派生类添加点代码,来解释第三种和第四种情况,: 

        [Help("BaseClass")]
        public class Base
        {
        }
    
        [Help("DeriveClass")]
        public class Derive : Base
        {
        } 

      现在我们来查询一下Help特性,我们只能得到派生类的属性,因为inherited被设置为true,但是AllowMultiple却被设置为false。因此基类的Help特性被派生类Help特性覆盖了。

                var type = typeof(Derive);
                //Assert.True(type.IsDefined(typeof(HelpAttribute),true));
                //Assert.False(type.IsDefined(typeof(HelpAttribute), false));
                Assert.True(type.GetCustomAttributes(true).Count() == 1);

      C# AttributeUsage的使用第四种情况:

      在这里,我们将会发现派生类既有基类的Help特性,也有自己的Help特性,因为AllowMultiple被设置为true。

                var type = typeof(Derive);
                //Assert.True(type.IsDefined(typeof(HelpAttribute),true));
                //Assert.False(type.IsDefined(typeof(HelpAttribute), false));
                Assert.True(type.GetCustomAttributes(true).Count() > 1);

      C# AttributeUsage的相关内容就向你介绍到这里,希望对你了解和掌握C# AttributeUsage的使用有所帮助。

  • 相关阅读:
    Notice: Only variable references should be returned by reference(PHP版本兼容性问题)
    App 开发:Hybrid 架构下的 HTML5 应用加速方案
    Hybrid App是如何实现网页语言与程序语言的混合?谁占主体?
    前端切图+网页排版的注意事项和经验分享
    php提示 Notice: Use of undefined constant name
    如何预测一个互联网产品的未来—一套关于产品的数学模型
    以 MAMP 为 Mac OS X 安装并设置 PHP开发环境
    关于EINTR错误的理解【转】
    socket中的函数遇见EINTR的处理【转】
    Ubuntu10.04中利用V4L2读取摄像头数据并保存成文件【转】
  • 原文地址:https://www.cnblogs.com/Hai--D/p/6125927.html
Copyright © 2020-2023  润新知