C#反射讀取類定製特性信息

此項目又分爲3個小項目:

1.WhatsNewAttributes類庫文件:用於定義定製特性類,標示跟新信息

2.myClass類庫文件:使用WhatsNew定製特性標記的類

3.Checker控制檯應用程序:用反射讀取myClass中的公共成員方法,以及定製特性

 

因此:myClass類庫需引入WhatsNewAttributes類庫編譯的DLL文件

         Checker控制檯應用程序需引入myClass類庫編譯的DLL文件^-^,WhatsNewAttributes類庫編譯的DLL文件

 

WhatsNewAttributes類庫文件

[c-sharp] view plaincopy
  1. using System;  
  2.   
  3. namespace WhatsNewAttributes  
  4. {  
  5.     /*  
  6.      * 特性類本身用一個特性System.AttributeUsage來標記,只能用在特性上,不能應用到類上 
  7.      * 該特性主要標示定製特性可應用到哪些類型的程序元素上 
  8.      *   
  9.      * AttributeTargets:指定定製特性可應用到哪些類型的程序元素上,可用"|"指定多個元素 
  10.      * AllowMultiple參數:表示一個特性是否可多次應用到同一項上 
  11.      * Inherited參數:表示應用到類,接口,方法,屬性上的特性可自動應用到所有派生類,接口,方法,屬性,重寫版本上 
  12.      *  
  13.      * 特性類派生於System.Attribute類 
  14.      */  
  15.   
  16.     /***************** 定義特性類LastModifiedAttribute ******************/  
  17.     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple=true, Inherited=false)]  
  18.     public class LastModifiedAttribute : Attribute  
  19.     {  
  20.         private DateTime _dateModified;  
  21.         private string _changes;  
  22.         private string _issues;  
  23.   
  24.         //特性類構造函數  
  25.         public LastModifiedAttribute(string dateModified, string changes)  
  26.         {  
  27.             this._dateModified = DateTime.Parse(dateModified);  
  28.             this._changes = changes;  
  29.         }  
  30.   
  31.         public DateTime DateModified  
  32.         {  
  33.             get { return _dateModified; }  
  34.         }  
  35.   
  36.         public string Changes  
  37.         {  
  38.             get { return _changes; }  
  39.         }  
  40.   
  41.         //特性類可選屬性  
  42.         public string Issues  
  43.         {  
  44.             get { return _issues; }  
  45.             set { _issues = value; }  
  46.         }  
  47.     }  
  48.   
  49.     /***************** 定義特性類SupportsWhatsNewAttribute,指示該特性僅用於程序集 ******************/  
  50.     [AttributeUsage(AttributeTargets.Assembly)]  
  51.     public class SupportsWhatsNewAttribute : Attribute  
  52.     {  
  53.     }  
  54. }  

 

myClass類庫文件

[c-sharp] view plaincopy
  1. using System;  
  2. //添加創建特性類生成的引用,包含其命名空間,這樣編譯器才能識別這些特性  
  3. using WhatsNewAttributes;  
  4.   
  5. //用SupportsWhatsNes特性標記程序集本身  
  6. [assembly : SupportsWhatsNew]  
  7. namespace myClass  
  8. {  
  9.     //在類上使用定製特性  
  10.     [LastModified("14 Feb 2008""改良了XX功能1")]  
  11.     [LastModified("16 Jun 2010""改良了XX功能2")]  
  12.     public class myClass  
  13.     {  
  14.         private string _my;  
  15.   
  16.         public string My  
  17.         {  
  18.             get { return _my; }  
  19.             set { _my = value; }  
  20.         }  
  21.   
  22.         //在方法上使用定製特性,添加可選屬性Issues  
  23.         [LastModified("17 Jun 2010""改良:從寫了基類的ToString()方法", Issues="這是個突出問題")]  
  24.         public override string ToString()  
  25.         {  
  26.             return "新更新的重寫方法";  
  27.         }  
  28.   
  29.         //私有方法  
  30.         private void myFun()  
  31.         { }  
  32.     }  
  33. }  

 

Checker控制檯應用程序

[c-sharp] view plaincopy
  1. using System;  
  2. using System.Reflection;  
  3. //添加創建特性類生成的引用,包含其命名空間,這樣編譯器才能識別這些特性  
  4. using WhatsNewAttributes;  
  5.   
  6. namespace Checker  
  7. {  
  8.     class Checker  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             /* Assembly類:在System.Reflection命名空間定義,允許訪問給定程序集的元數據 
  13.              *   1.使用Assembly實例前,需要加入相應程序集 
  14.              *     Assembly.Load():參數爲程序集名稱 
  15.              *     Assembly.LoadFrom():參數爲程序集的完整路徑名 
  16.              *   2.查找在程序集中定義的類型Assembly.GetTypes() 
  17.              *   3.查找程序集的定製特性Attribute.GetCustomAttributes(assemblyObj) 
  18.              *     定義定製特性時,必須爲他們編寫類,定製特性與對象一樣,加載了程序集後 
  19.              *     就可以讀取這些特定對象,查看他們的屬性,調用他們的方法 
  20.              *     a.所有特性都作爲一般Attribute引用來獲取 
  21.              *     b.如要調用爲定製特性定義的方法屬性,就要把這些引用顯示轉化爲相關定製特性類 
  22.              */   
  23.             Assembly theAssembly = Assembly.Load("myClass");  
  24.             Attribute supportsAttribute = Attribute.GetCustomAttribute(theAssembly,typeof(SupportsWhatsNewAttribute));  
  25.   
  26.             string Name = theAssembly.FullName;  
  27.   
  28.             if (supportsAttribute == null)  
  29.             {  
  30.                 Console.WriteLine("此程序集不支持SupportsWhatsNew特性");  
  31.                 return;  
  32.             }  
  33.             else  
  34.             {  
  35.                 Console.WriteLine("程序集:" + Name);  
  36.             }  
  37.   
  38.             //Assembly類的特性可以獲得在相應程序集中定義的所有類型的信息  
  39.             //調用GetTypes()方法,獲得此程序集中定義的類型數組,此處只定義了一個myClass類  
  40.             Type[] types = theAssembly.GetTypes();  
  41.        //注:此處如果只要同一項目下的某類的類型,可以直接使用typeof(ClassName)來代替
  42.             Console.WriteLine("信息總數:" + types.Length);         //1  
  43.   
  44.             foreach (Type definedType in types)  
  45.             {  
  46.                 //如果該definedType不是一個類  
  47.                 if (!(definedType.IsClass))  
  48.                 {  
  49.                     //退出程序  
  50.                     return;  
  51.                 }  
  52.                 else  
  53.                 {  
  54.                     Console.WriteLine("成員名:" + definedType.Name);          //myClass  
  55.                 }  
  56.   
  57.                 //檢索myClass類的自定義屬性數組  
  58.                 Attribute[] attribs = Attribute.GetCustomAttributes(definedType);  
  59.   
  60.                 if (attribs.Length == 0)  
  61.                 {  
  62.                     Console.WriteLine("這個類沒有改良過");  
  63.                 }  
  64.                 else  
  65.                 {  
  66.                     foreach (Attribute attrib in attribs)  
  67.                     {  
  68.                         WriteAttributeInfo(attrib);  
  69.                     }  
  70.                 }  
  71.   
  72.                 //檢索myClass類的所有公共方法數組  
  73.                 MemberInfo[] methods = definedType.GetMethods();  
  74.                 Console.WriteLine("修改過的方法:");  
  75.   
  76.                 foreach (MethodInfo nextMethod in methods)  
  77.                 {  
  78.                     //檢索myClass類的當前公共方法的由LastModifiedAttribute標示的定製屬性數組  
  79.                     object[] attribs2 = nextMethod.GetCustomAttributes(typeof(LastModifiedAttribute), false);  
  80.   
  81.                     if (attribs2 != null)  
  82.                     {  
  83.                         Console.WriteLine( nextMethod.ReturnType + " " + nextMethod.Name + "()");  
  84.   
  85.                         foreach(Attribute nextAttrib in attribs2)  
  86.                         {  
  87.                             WriteAttributeInfo(nextAttrib);  
  88.                         }  
  89.                     }  
  90.                 }  
  91.             }  
  92.   
  93.             Console.ReadLine();  
  94.         }  
  95.   
  96.         //顯示該自定義屬性的信息  
  97.         public static void WriteAttributeInfo(Attribute attrib)  
  98.         {  
  99.             //定義特性類LastModifiedAttribute就是爲了方便實例化後訪問類中個屬性  
  100.             LastModifiedAttribute lastModifiedAttrib = attrib as LastModifiedAttribute;  
  101.   
  102.             if (lastModifiedAttrib == null)  
  103.             {  
  104.                 return;  
  105.             }  
  106.   
  107.             DateTime modifiedDate = lastModifiedAttrib.DateModified;  
  108.   
  109.             Console.WriteLine("改良:" + modifiedDate.ToLongDateString() + "," + lastModifiedAttrib.Changes);  
  110.   
  111.             if (lastModifiedAttrib.Issues != null)  
  112.             {  
  113.                 Console.WriteLine("突出問題:" + lastModifiedAttrib.Issues);  
  114.             }  
  115.         }  
  116.     }  
  117. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章