自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#反射訪問屬性規(guī)范及示例

開發(fā) 后端
C#具有一個反射系統(tǒng),可用來檢索用自定義屬性定義的信息,本文介紹其主要方法。

如果沒有檢索自定義屬性的信息和對其進(jìn)行操作的方法,則定義自定義屬性并將其放置在源代碼中就沒有意義。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ìn)行迭代,確定根據(jù)每個數(shù)組元素的類型所應(yīng)用的屬性,并從屬性對象中提取信息。

C#反射——示例

下面是一個完整的示例。定義一個自定義屬性,將其應(yīng)用于若干實體并通過反射進(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#反射訪問屬性的問題就介紹到這里。

【編輯推薦】

  1. C#3.5新特性的介紹
  2. C#中DirectSound錄音的使用
  3. C#擴展方法性能測試對比
  4. C#多標(biāo)簽瀏覽器功能的擴展
  5. C#標(biāo)簽的制作:多標(biāo)簽的實現(xiàn)
責(zé)任編輯:book05 來源: hi.baidu
相關(guān)推薦

2024-09-23 16:55:18

C#代碼開發(fā)

2021-03-15 08:18:23

C#反射模塊

2024-05-20 00:00:00

C#屬性Property

2009-08-25 15:50:13

C#連接遠(yuǎn)程數(shù)據(jù)庫

2024-09-23 21:00:57

C#反射

2009-09-07 03:37:51

C#窗體

2009-08-27 15:53:30

C#中using wo

2009-09-01 17:41:45

C# HelpAttr

2009-08-28 13:56:25

C#反射命名空間

2024-03-04 18:49:59

反射C#開發(fā)

2024-04-15 04:00:00

C#反射代碼

2009-08-21 08:41:44

C#反射

2009-09-03 11:00:29

C#反射機制

2024-06-18 08:37:25

場景異步編程代碼

2024-06-24 03:00:00

2009-08-27 16:30:08

C#編程命名規(guī)范

2009-09-01 10:37:51

C#項目代碼C#代碼規(guī)范

2009-08-21 08:59:35

C#語言規(guī)范

2024-05-17 08:42:52

AttributeMyClass方法

2009-08-13 13:38:30

C#命名規(guī)范
點贊
收藏

51CTO技術(shù)棧公眾號