C# 特性詳解

來源:互聯網
上載者:User

  特性(attribute)是被指定給某一聲明的一則附加的聲明性資訊。

    在C#中,有一個小的預定義特性集合。在學習如何建立我們自己的定製特性(custom attributes)之前,我們先來看看在我們的代碼中如何使用預定義特性。

 

 1 using System;  2 public class AnyClass  3 {  4     [Obsolete("Don't use Old method, use New method", true)]  5     static void Old( ) { }   6     static void New( ) { }  7     public static void Main( )  8     {  9         Old( ); 10     } 11 } 

 

    我們先來看一下上面這個例子,在這個例子中我們使用了Obsolete特性,它標記了一個不應該再被使用的程式實體。第一個參數是一個字串,它解釋了為什麼該實體是過時的以及應該用什麼實體來代替它。實際上,你可以在這裡寫任何文本。第二個參數告訴編譯器應該把使用這個過時的程式實體當作一種錯誤。它的預設值是false,也就是說編譯器對此會產生一個警告。

    當我們嘗試編譯上面這段程式的時候,我們將會得到一個錯誤:   

    AnyClass.Old()' is obsolete: 'Don't use Old method, use New method'   

 

    開發定製特性(custom attributes)

    現在讓我們來看看如何開發我們自己的特性。 
    首先我們要從System.Attribute派生出我們自己的屬性類別(一個從System.Attribute抽象類別繼承而來的類,不管是直接還是間接繼承,都會成為一個屬性類別。屬性類別的聲明定義了一種可以被放置在聲明之上新的特性)。

 

1 using System; 2 public class HelpAttribute : Attribute 3 { 4 } 

 

  不管你是否相信,我們已經建立了一個定製特性,現在我們可以用它來裝飾現有的類就好像上面我們使用Obsolete attribute一樣。

 

1 [Help()] 2 public class AnyClass 3 { 4 } 

 

  注意:對一個屬性類別名使用Attribute尾碼是一個慣例。然而,當我們把特性添加到一個程式實體,是否包括 Attribute尾碼是我們的自由。編譯器會首先在System.Attribute的衍生類別中尋找被添加的屬性類別。如果沒有找到,那麼編譯器會添加 Attribute尾碼繼續尋找。

    到目前為止,這個特性還沒有起到什麼作用。下面我們來添加些東西給它使它更有用些。

 

 1 using System;  2 public class HelpAttribute : Attribute  3 {  4     public HelpAttribute(String Descrition_in)  5     {  6         this.description = Description_in;  7     }  8     protected String description;  9     public String Description 10     { 11         get 12         { 13             return this.description; 14         } 15     } 16 } 17 [Help("this is a do-nothing class")] 18 public class AnyClass 19 { 20 } 

   

    在上面的例子中,我們給HelpAttribute屬性類別添加了一個屬性並且在後續的部分中我們會在運行時環境中查尋它。

 

    定義或控制特性的使用

    AttributeUsage類是另外一個預定義屬性類別,它協助我們控制我們自己的定製特性的使用。它描述了一個定製特性如和被使用。 
    AttributeUsage有三個屬性,我們可以把它放置在定製屬性前面。第一個屬性是: 

 

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


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

 

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

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

 

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

 

    先讓我們來看一下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 } 

 

    編譯器報告錯誤如下:

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

 

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

    Assembly,Module,Class,Struct,Enum,Constructor,Method,Property,Field,Event,Interface,Parameter,Delegate

    All = Assembly | Module | Class | Struct | Enum | Constructor | Method | Property | Field | Event | Interface | Parameter | Delegate

    ClassMembers = Class | Struct | Enum | Constructor | Method | Property | Field | 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     } 9 } 

 

    它產生了一個編譯期錯誤。 
    AnyClass.cs: Duplicate 'Help' attribute

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.