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

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

新聞中心

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

如果沒(méi)有檢索自定義屬性的信息和對(duì)其進(jìn)行操作的方法,則定義自定義屬性并將其放置在源代碼中就沒(méi)有意義。C# 具有一個(gè)反射系統(tǒng),可用來(lái)檢索用自定義屬性定義的信息。主要方法是 GetCustomAttributes,它返回對(duì)象數(shù)組,這些對(duì)象在運(yùn)行時(shí)等效于源代碼屬性。此方法具有多個(gè)重載版本。有關(guān)更多信息,請(qǐng)參見(jià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; 

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

C#反射——示例

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

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èn)屬性的問(wèn)題就介紹到這里。


分享名稱(chēng):C#反射訪問(wèn)屬性規(guī)范及示例
文章分享:http://m.5511xx.com/article/dhehddj.html