C# AttributeUsage的使用淺析(轉載)

  • c# AttributeUsage的使用在我們開發中是十分常見的,那麼我們瞭解c# AttributeUsage的基本情況從何入手呢?那麼本文就向你詳細介紹相關的內容。

C# AttributeUsage的使用是如何的呢?首先讓我們來了解一下什麼是AttributeUsage類它是另外一個預定義特性類,AttributeUsage類的作用就是幫助我們控制定製特性的使用。其實AttributeUsage類就是描述了一個定製特性如和被使用。

C# AttributeUsage的使用要明白:

AttributeUsage有三個屬性,我們可以把它放置在定製屬性前面。第一個屬性是:

◆ValidOn
通過這個屬性,我們能夠定義定製特性應該在何種程序實體前放置。一個屬性可以被放置的所有程序實體在AttributeTargets enumerator中列出。通過OR操作我們可以把若干個AttributeTargets值組合起來。

◆AllowMultiple 

這個屬性標記了我們的定製特性能否被重複放置在同一個程序實體前多次。

◆Inherited
我們可以使用這個屬性來控制定製特性的繼承規則。它標記了我們的特性能否被繼承。

C# AttributeUsage的使用實例:

下面讓我們來做一些實際的東西。我們將會在剛纔的Help特性前放置AttributeUsage特性以期待在它的幫助下控制Help特性的使用。

  1. using System;  
  2. [AttributeUsage(AttributeTargets.Class), AllowMultiple = false,  
  3. Inherited = false ]  
  4. public class HelpAttribute : Attribute  
  5. {  
  6. public HelpAttribute(String Description_in)  
  7. {  
  8. this.description = Description_in;  
  9. }  
  10. protected String description;  
  11. public String Description  
  12. {  
  13. get  
  14. {  
  15. return this.description;  
  16. }  
  17. }  
  18. }

先讓我們來看一下AttributeTargets.Class。它規定了Help特性只能被放在class的前面。這也就意味着下面的代碼將會產生錯誤:

  1. [Help("this is a do-nothing class")]  
  2. public class AnyClass  
  3. {  
  4. [Help("this is a do-nothing method")] //error  
  5. public void AnyMethod()  
  6. {  
  7. }  
  8. }

編譯器報告錯誤如下:

  1. AnyClass.cs: Attribute 'Help' is not valid on this declaration type. 
  2.  
  3. It is valid on 'class' declarations only. 

我們可以使用AttributeTargets.All來允許Help特性被放置在任何程序實體前。可能的值是:

  1. Assembly,  
  2. Module,  
  3. Class,  
  4. Struct,  
  5. Enum,  
  6. Constructor,  
  7. Method,  
  8. Property,  
  9. Field,  
  10. Event,  
  11. Interface,  
  12. Parameter,  
  13. Delegate,  
  14. All = Assembly | Module | Class |  
  15. Struct | Enum | Constructor |  
  16. Method | Property | Field | Event |  
  17. Interface | Parameter | Delegate,  
  18. ClassMembers = Class | Struct | Enum | 
  19. Constructor | Method | Property | Field | 
  20. Event | Delegate | Interface )

下面考慮一下AllowMultiple = false。它規定了特性不能被重複放置多次。

  1. [Help("this is a do-nothing class")]  
  2. [Help("it contains a do-nothing method")]  
  3. public class AnyClass  
  4. {  
  5. [Help("this is a do-nothing method")] //error  
  6. public void AnyMethod()  
  7. {  
  8. }  

它產生了一個編譯期錯誤。

  1. AnyClass.cs: Duplicate 'Help' attribute

Ok,現在我們來討論一下最後的這個屬性。Inherited, 表明當特性被放置在一個基類上時,它能否被派生類所繼承。

  1. [Help("BaseClass")]  
  2. public class Base  
  3. {  
  4. }  
  5.   
  6. public class Derive : Base  
  7. {  
  8. }

C# AttributeUsage的使用會有四種可能的組合:

  1. [AttributeUsage(AttributeTargets.Class, 
  2. AllowMultiple = false, Inherited = false ]  
  3. [AttributeUsage(AttributeTargets.Class,  
  4. AllowMultiple = true, Inherited = false ]  
  5. [AttributeUsage(AttributeTargets.Class,  
  6. AllowMultiple = false, Inherited = true ]  
  7. [AttributeUsage(AttributeTargets.Class,  
  8. AllowMultiple = true, Inherited = true ]

C# AttributeUsage的使用第一種情況:

如果我們查詢(Query)(稍後我們會看到如何在運行期查詢一個類的特性)Derive類,我們將會發現Help特性並不存在,因爲inherited屬性被設置爲false。

C# AttributeUsage的使用第二種情況:

和第一種情況相同,因爲inherited也被設置爲false。

C# AttributeUsage的使用第三種情況:

爲了解釋第三種和第四種情況,我們先來給派生類添加點代碼:

  1. [Help("BaseClass")]  
  2. public class Base  
  3. {  
  4. }  
  5. [Help("DeriveClass")]  
  6. public class Derive : Base  
  7. {  
  8. }

現在我們來查詢一下Help特性,我們只能得到派生類的屬性,因爲inherited被設置爲true,但是AllowMultiple卻被設置爲false。因此基類的Help特性被派生類Help特性覆蓋了。

C# AttributeUsage的使用第四種情況:

在這裏,我們將會發現派生類既有基類的Help特性,也有自己的Help特性,因爲AllowMultiple被設置爲true。

C# AttributeUsage的相關內容就向你介紹到這裏,希望對你瞭解和掌握C# AttributeUsage的使用有所幫助。

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