特性(attribute)是被指定給某一聲明的一則附加的聲明性信息。
在C#中,有一個小的預定義特性集合。在學習如何建立我們自己的定製特性(custom attributes)之前,我們先來看看在我們的代碼中如何使用預定義特性。
- using System;
- public class AnyClass
- {
- [Obsolete("Don't use Old method, use New method", true)]
- static void Old( ) { }
- static void New( ) { }
- public static void Main( )
- {
- Old( );
- }
- }
當我們嘗試編譯上面這段程序的時候,我們將會得到一個錯誤:
AnyClass.Old()' is obsolete: 'Don't use Old method, use New method'
開發定製特性(custom attributes)
現在讓我們來看看如何開發我們自己的特性。
首先我們要從System.Attribute派生出我們自己的特性類(一個從System.Attribute抽象類繼承而來的類,不管是直接還是間接繼承,都會成爲一個特性類。特性類的聲明定義了一種可以被放置在聲明之上新的特性)。
- using System;
- public class HelpAttribute : Attribute
- {
- }
- [Help()]
- public class AnyClass
- {
- }
到目前爲止,這個特性還沒有起到什麼作用。下面我們來添加些東西給它使它更有用些。
- using System;
- public class HelpAttribute : Attribute
- {
- public HelpAttribute(String Descrition_in)
- {
- this.description = Description_in;
- }
- protected String description;
- public String Description
- {
- get
- {
- return this.description;
- }
- }
- }
- [Help("this is a do-nothing class")]
- public class AnyClass
- {
- }
定義或控制特性的使用
AttributeUsage類是另外一個預定義特性類,它幫助我們控制我們自己的定製特性的使用。它描述了一個定製特性如和被使用。
AttributeUsage有三個屬性,我們可以把它放置在定製屬性前面。第一個屬性是:
ValidOn
通過這個屬性,我們能夠定義定製特性應該在何種程序實體前放置。一個屬性可以被放置的所有程序實體在AttributeTargets enumerator中列出。通過OR操作我們可以把若干個AttributeTargets值組合起來。
AllowMultiple
這個屬性標記了我們的定製特性能否被重複放置在同一個程序實體前多次。
Inherited
我們可以使用這個屬性來控制定製特性的繼承規則。它標記了我們的特性能否被繼承。
下面讓我們來做一些實際的東西。我們將會在剛纔的Help特性前放置AttributeUsage特性以期待在它的幫助下控制Help特性的使用。
- using System;
- [AttributeUsage(AttributeTargets.Class), AllowMultiple = false,
- Inherited = false ]
- public class HelpAttribute : Attribute
- {
- public HelpAttribute(String Description_in)
- {
- this.description = Description_in;
- }
- protected String description;
- public String Description
- {
- get
- {
- return this.description;
- }
- }
- }
- [Help("this is a do-nothing class")]
- public class AnyClass
- {
- [Help("this is a do-nothing method")] //error
- public void AnyMethod()
- {
- }
- }
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。它規定了特性不能被重複放置多次。
- [Help("this is a do-nothing class")]
- [Help("it contains a do-nothing method")]
- public class AnyClass
- {
- [Help("this is a do-nothing method")] //error
- public void AnyMethod()
- {
- }
- }
AnyClass.cs: Duplicate 'Help' attribute
Ok,現在我們來討論一下最後的這個屬性。Inherited, 表明當特性被放置在一個基類上時,它能否被派生類所繼承。
- [Help("BaseClass")]
- public class Base
- {
- }
- public class Derive : Base
- {
- }
- [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false ]
- [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false ]
- [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true ]
- [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true ]
如果我們查詢(Query)(稍後我們會看到如何在運行期查詢一個類的特性)Derive類,我們將會發現Help特性並不存在,因爲inherited屬性被設置爲false。
第二種情況:
和第一種情況相同,因爲inherited也被設置爲false。
第三種情況:
爲了解釋第三種和第四種情況,我們先來給派生類添加點代碼:
- [Help("BaseClass")]
- public class Base
- {
- }
- [Help("DeriveClass")]
- public class Derive : Base
- {
- }
第四種情況:
在這裏,我們將會發現派生類既有基類的Help特性,也有自己的Help特性,因爲AllowMultiple被設置爲true。
定義或控制特性的使用AttributeUsage類是另外一個預定義特性類,它幫助我們控制我們自己的定製特性的使用。它描述了一個定製特性如何被使用。