日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#反射訪問屬性規(guī)范及示例

如果沒有檢索自定義屬性的信息和對其進行操作的方法,則定義自定義屬性并將其放置在源代碼中就沒有意義。C# 具有一個反射系統(tǒng),可用來檢索用自定義屬性定義的信息。主要方法是 GetCustomAttributes,它返回對象數(shù)組,這些對象在運行時等效于源代碼屬性。此方法具有多個重載版本。有關(guān)更多信息,請參見 Attribute。

C#反射——屬性規(guī)范

C#

 
 
 
  1. [Author("H. Ackerman", version = 1.1)]  
  2. class SampleClass 

在概念上等效于:

C#

 
 
 
  1. Author anonymousAuthorObject = new Author("H. Ackerman");  
  2. anonymousAuthorObject.version = 1.1; 

但是,直到查詢 SampleClass 以獲取屬性時才會執(zhí)行此代碼。對 SampleClass 調(diào)用 GetCustomAttributes 會導(dǎo)致按上述方式構(gòu)造并初始化一個 Author 對象。如果類還有其他屬性,則其他屬性對象的以類似方式構(gòu)造。然后 GetCustomAttributes 返回 Author 對象和數(shù)組中的任何其他屬性對象。之后就可以對此數(shù)組進行迭代,確定根據(jù)每個數(shù)組元素的類型所應(yīng)用的屬性,并從屬性對象中提取信息。

C#反射——示例

下面是一個完整的示例。定義一個自定義屬性,將其應(yīng)用于若干實體并通過反射進行檢索。

C#

 
 
 
  1. [System.AttributeUsage(System.AttributeTargets.Class |  
  2.                        System.AttributeTargets.Struct,  
  3.                        AllowMultiple = true)  // multiuse attribute  
  4. ]  
  5. public class Author : System.Attribute  
  6. {  
  7.     string name;  
  8.     public double version;  
  9.  
  10.     public Author(string name)  
  11.     {  
  12.         this.name = name;  
  13.         version = 1.0;  // Default value  
  14.     }  
  15.  
  16.     public string GetName()  
  17.     {  
  18.         return name;  
  19.     }  
  20. }  
  21.  
  22. [Author("H. Ackerman")]  
  23. private class FirstClass  
  24. {  
  25.     // ...  
  26. }  
  27.  
  28. // No Author attribute  
  29. private class SecondClass  
  30. {  
  31.     // ...  
  32. }  
  33.  
  34. [Author("H. Ackerman"), Author("M. Knott", version = 2.0)]  
  35. private class ThirdClass  
  36. {  
  37.     // ...  
  38. }  
  39.  
  40. class TestAuthorAttribute  
  41. {  
  42.     static void Main()  
  43.     {  
  44.         PrintAuthorInfo(typeof(FirstClass));  
  45.         PrintAuthorInfo(typeof(SecondClass));  
  46.         PrintAuthorInfo(typeof(ThirdClass));  
  47.     }  
  48.  
  49.     private static void PrintAuthorInfo(System.Type t)  
  50.     {  
  51.         System.Console.WriteLine("Author information for {0}", t);  
  52.         System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t);  // reflection  
  53.  
  54.         foreach (System.Attribute attr in attrs)  
  55.         {  
  56.             if (attr is Author)  
  57.             {  
  58.                 Author a = (Author)attr;  
  59.                 System.Console.WriteLine("   {0}, version {1:f}", a.GetName(), a.version);  
  60.             }  
  61.         }  
  62.     }  

輸出

Author information for FirstClass

H. Ackerman, version 1.00

Author information for SecondClass

Author information for ThirdClass

H. Ackerman, version 1.00

M. Knott, version 2.00

本文關(guān)于C#反射訪問屬性的問題就介紹到這里。


網(wǎng)站題目:C#反射訪問屬性規(guī)范及示例
本文URL:http://www.5511xx.com/article/dhehddj.html