2.C#自定义Attribute

阅读目录 
   一:
C#自定义Attribute

   二:AttributeUsageAttribute中的3个属性(Property)中的AttributeTargets
   三:AttributeUsageAttribute中的3个属性(Property)中的,AllowMultiple
   四:AttributeUsageAttribute中的3个属性(Property)中的Inherited

 
一:C#自定义Attribute
  
写一个类继承自System.Attribute就可以了
  二:AttributeUsageAttribute中的3个属性(Property)中的AttributeTargets
     
这个属性类能应用于哪种类型上面,如下图,我的例子中声明属性类HelperAttribute只能用于interface接口上面,而我实际应用于class上面编译就报错了说声明类型不对
 
  
  

 三:AttributeUsageAttribute中的3个属性(Property)中的,AllowMultiple
   
能否多次声明指定的属性,如下图AllowMultiple=false的话,我在一个类上声明2次,编辑就报错了

 

四:AttributeUsageAttribute中的3个属性(Property)中的Inherited
我们在父类上声明属性类,而在子类上不声明属性类,把属性类设置为Inherited = false,我们看到查找SubMyClass1类型没有找到它的父类MyClass1的HelperAttribute属性类,所以没有任何输出

我们改为Inherited = true后,再调试看到查找SubMyClass1类型找到了它父类的HelperAttribute属性类,然后输出描述

 我们在父类上声明属性类,也在子类上声明属性类,把属性类设置为 AllowMultiple = false, Inherited = true,我们看到查找SubMyClass1类型找到了它自己的HelperAttribute属性类,然后输出描述,没有找到父类MyClass1的HelperAttribute属性类,是因为 AllowMultiple = false不允许多重属性,所以父类的HelperAttribute属性类被子类SubMyClass1的HelperAttribute属性类覆盖了,所以最终只能找到子类的属性类

我们再改为AllowMultiple = true, Inherited = true,我们看到查找SubMyClass1类型不但是找到了它自己的HelperAttribute属性类而且找到了父类MyClass1的HelperAttribute属性类,所以最终会输出两条信息

实例代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Reflection;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 
  8 namespace CustomizeAttribute
  9 {
 10     class Program
 11     {
 12         static void Main(string[] args)
 13         {
 14             foreach (Attribute attr in typeof(SubMyClass1).GetCustomAttributes(true))
 15             {
 16                 HelperAttribute helperAtt = attr as HelperAttribute;
 17                 if (helperAtt != null)
 18                 {
 19                     Console.WriteLine("Name:{0} Description:{1}",helperAtt.Name, helperAtt.Description);
 20                 }
 21             }
 22 
 23             Console.ReadLine();
 24         }
 25     }
 26 
 27     [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
 28     public class HelperAttribute : System.Attribute
 29     {
 30         private string _name;
 31         private string _description;
 32 
 33         public HelperAttribute(string des)
 34         {
 35             this._name = "Default name";
 36             this._description = des;
 37         }
 38 
 39         public HelperAttribute(string des1,string des2)
 40         {
 41             this._description = des1;
 42             this._description = des2;
 43         }
 44 
 45         public string Description
 46         {
 47             get
 48             {
 49                 return this._description;
 50             }
 51             set
 52             {
 53                 this._description = value;
 54             }
 55         }
 56 
 57         public string Name
 58         {
 59             get
 60             {
 61                 return this._name;
 62             }
 63             set
 64             {
 65                 this._name = value;
 66             }
 67         }
 68 
 69         public string PrintMessage()
 70         {
 71             return "PrintMessage";
 72         }
 73     }
 74 
 75     [HelperAttribute("this is my class1")]
 76     public class MyClass1
 77     {
 78 
 79     }
 80 
 81     public class SubMyClass1 : MyClass1
 82     {
 83 
 84     }
 85     
 86 
 87     [HelperAttribute("this is my class2", Name = "myclass2")]
 88     public class MyClass2
 89     {
 90 
 91     }
 92 
 93     [HelperAttribute("this is my class3", Name = "myclass3", Description = "New Description")]
 94     public class MyClass3
 95     {
 96 
 97     }
 98 
 99     [HelperAttribute("this is my class4", "this is my class4")]
100     public class MyClass4
101     {
102 
103     }
104 }
posted @ 2016-03-10 15:36  David.Meng  阅读(5138)  评论(1编辑  收藏  举报