AttributeUsage

预定义特性 AttributeUsage 描述了如何使用一个自定义特性类。它规定了特性可应用到的项目的类型。

规定该特性的语法如下:

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

validon:自定义特性的对象,可以是类、方法、属性等对象(默认值是 AttributeTargets.All)
AllowMultiple:是否允许被多次使用(默认值为false:单用的)
Inherited:是否可被派生类继承(默认值为false:不能)


下面请看使用:
using System;

namespace AttributeUsagePractice
{
    [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; }
        }
    }

    
    class Program
    {
        [Help("this is a main class")]  //error
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            
            // TODO: Implement Functionality Here
            
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
含义为:定制特性类,不允许多次使用,不能被继承

第一个参数:
因为它的特性目标是 AttributeTargets.Class,而它放在函数前面,所以上面的程序会报错:特性“Help”对此声明类型无效。它只对“class”声明有效,正确的做法是放在 class Program 上面。


如果是下面的代码:
using System;

namespace AttributeUsagePractice
{
    [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; }
        }
    }

    [Help("this is a main class")]
    [Help("this is a main2 class")]  //error
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            
            // TODO: Implement Functionality Here
            
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}
第二个参数:

因为AllowMultiple = false,上面多次使用,所以报错 重复的“Help”特性,正确的做法就是去掉它



using System;
using System.Linq;

namespace AttributeUsagePractice
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    
    public class HelpAttribute : Attribute
    {
        public HelpAttribute(){}
        
        public HelpAttribute(String Description_in)
        {
            this.description = Description_in;
        }
        
        protected String description;
        
        public String Description
        {
            get { return this.description; }
        }
    }

    [Help("this is a HelpAttribute use class")]
    public class UseHelpAttribute
    {
        
    }
    
    
    public class UseHelpAttributeDerive : UseHelpAttribute
    {
        
    }
    
    
    class Program
    {
        public static void Main(string[] args)
        {
            // TODO: Implement Functionality Here
            
            UseHelpAttributeDerive objHelpAttribute = new UseHelpAttributeDerive();
            
            Type t = objHelpAttribute.GetType();
                
            object [] objAttrs = t.GetCustomAttributes(typeof(HelpAttribute),true);
            
            if(objAttrs!= null && objAttrs.Length > 0)
            {
                object temp = objAttrs.First();
                HelpAttribute myAttr = temp as HelpAttribute;
                Console.WriteLine("类描述:{0}", myAttr.Description);
            }
            else
            {
                Console.WriteLine("没有类描述");
            }
            
            
            Console.ReadKey(true);
        }
    }
}
 
第三个参数:

因为Inherited = false,所以运行结果为;

C# 特性AttributeUsage的理解与使用_自定义

 

如果把Inherited = false 改为 Inherited = true,效果如下:

C# 特性AttributeUsage的理解与使用_编程_02