首先要說的是,可能一些剛接觸C#的朋友常常容易把屬性(Property)跟特性(Attribute)弄混淆,其實這是兩種不同的東西。屬性就是面向對象思想里所說的封裝在類里面的數據字段,其形式為:
1: public class HumanBase
2: {
3: public string Name { get; set; }
4: public int Age { get; set; }
5: public int Gender { get; set; }
6: }
在HumanBase這個類里出現的字段都叫屬性(Property),而特性(Attribute)又是怎樣的呢?
1: [Serializable]
2: public class HumanBase
3: {
4: public string Name { get; set; }
5: public int Age { get; set; }
6: public int Gender { get; set; }
7: }
簡單地講,我們在HumanBase類聲明的上一行加了一個[Serializable],這就是特性(Attribute),它表示HumanBase是可以被序列化的,這對於網絡傳輸是很重要的,不過你不用擔心如何去理解它,如何理解就是我們下面要探討的。
C#的特性可以應用於各種類型和成員。前面的例子將特性用在類上就可以被稱之為“類特性”,同理,如果是加在方法聲明前面的就叫方法特性。無論它們被用在哪里,無論它們之間有什么區別,特性的最主要目的就是自描述。並且因為特性是可以由自己定制的,而不僅僅局限於.NET提供的那幾個現成的,因此給C#程序開發帶來了相當大的靈活性和便利。
我們還是借用生活中的例子來介紹C#的特性機制吧。
假設有一天你去坐飛機,你就必須提前去機場登機處換登機牌。登機牌就是一張紙,上面寫着哪趟航班、由哪里飛往哪里以及你的名字、座位號等等信息,其實,這就是特性。它不需要你生理上包含這些屬性(人類出現那會兒還沒飛機呢),就像上面的HumanBase類沒有IsSerializable這樣的屬性,特性只需要在類或方法需要的時候加上去就行了,就像你不總是在天上飛一樣。
當我們想知道HumanBase是不是可序列化的,可以通過:
1: static void Main(string[] args)
2: {
3: Console.WriteLine(typeof(HumanBase).IsSerializable);
4:
5: Console.ReadLine();
6: }
拿到了登機牌,就意味着你可以合法地登機起飛了。但此時你還不知道你要坐的飛機停在哪里,不用擔心,地勤人員會開車送你過去,但是他怎么知道你是哪趟航班的呢?顯然還是通過你手中的登機牌。所以,特性最大的特點就是自描述。
既然是起到描述的作用,那目的就是在於限定。就好比地勤不會把你隨便拉到一架飛機跟前就扔上去了事,因為標簽上的說明信息就是起到限定的作用,限定了目的地、乘客和航班,任何差錯都被視為異常。如果前面的HumanBase不加上Serializable特性就不能在網絡上傳輸。
我們在順帶來介紹一下方法特性,先給HumanProperty加上一個Run方法:
1: [Serializable]
2: public class HumanPropertyBase
3: {
4: public string Name { get; set; }
5: public int Age { get; set; }
6: public int Gender { get; set; }
7:
8: public void Run(int speed)
9: {
10: // Running is good for health.
11: }
12: }
只要是個四肢健全、身體健康的人就可以跑步,那這么說,跑步就是有前提條件的,至少是四肢健全,身體健康。由此可見,殘疾人和老年人如果跑步就會出問題。假設一個HumanBase的對象代表的是一位耄耋老人,如果讓他當劉翔的陪練,那就直接光榮了。如何避免這樣的情況呢,我們可以在Run方法中加一段邏輯代碼,先判斷Age大小,如果小於2或大於60直接拋異常,但是2-60歲之間也得用Switch來分年齡階段地判斷speed參數是否合適,那么邏輯就相當臃腫。簡而言之,如何用特性表示一個方法不能被使用呢?OK, here we go:
1: [Obsolete("I'm so old, don't kill me!", true)]
2: public virtual void Run(int speed)
3: {
4: // Running is good for health.
5: }
上面大致介紹了一下特性的使用與作用,接下來我們要向大家展示的是如何通過自定義特性來提高程序的靈活性,如果特性機制僅僅能使用.NET提供的那幾種特性,不就太不過癮了么。
首先,特性也是類。不同於其它類的是,特性都必須繼承自System.Attribute類,否則編譯器如何知道誰是特性誰是普通類呢。當編譯器檢測到一個類是特性的時候,它會識別出其中的信息並存放在元數據當中,僅此而已,編譯器並不關心特性說了些什么,特性也不會對編譯器起到任何作用,正如航空公司並不關心每個箱子要去哪里,只有箱子的主人和搬運工才會去關心這些細節。假設我們現在就是航空公司的管理人員,需要設計出前面提到的登機牌,那么很簡單,我們先看看最主要的信息有哪些:
1: public class BoardingCheckAttribute : Attribute
2: {
3: public string ID { get; private set; }
4: public string Name { get; private set; }
5: public int FlightNumber { get; private set; }
6: public int PostionNumber { get; private set; }
7: public string Departure { get; private set; }
8: public string Destination { get; private set; }
9: }
我們簡單列舉這些屬性作為航空公司登機牌上的信息,用法和前面的一樣,貼到HumanBase上就行了,說明此人具備登機資格。這里要簡單提一下,你可能已經注意到了,在使用BoardingCheckAttribute的時候已經把Attribute省略掉了,不用擔心,這樣做是對的,因為編譯器默認會自己加上然后查找這個屬性類的。哦,等一下,我突然想起來他該登哪架飛機呢?顯然,在這種需求下,我們的特性還沒有起到應有的作用,我們還的做點兒工作,否則乘客面對一張空白的機票一定會很迷茫。
於是,我們必須給這個特性加上構造函數,因為它不僅僅表示登機的資格,還必須包含一些必要的信息才行:
1: public BoardingCheckAttribute(string id, string name, string flightNumber, string positionNumber, string departure, string destination)
2: {
3: this.ID = id;
4: this.Name = name;
5: this.FlightNumber = flightNumber;
6: this.PositionNumber = positionNumber;
7: this.Departure = departure;
8: this.Destination = destination;
9: }
OK,我們的乘客就可以拿到一張正式的登機牌登機了,have a good flight!
1: static void Main(string[] args)
2: {
3: BoardingCheckAttribute boardingCheck = null;
4: object[] customAttributes = typeof(HumanPropertyBase).GetCustomAttributes(true);
5:
6: foreach (var attribute in customAttributes)
7: {
8: if (attribute is BoardingCheckAttribute)
9: {
10: boardingCheck = attribute as BoardingCheckAttribute;
11:
12: Console.WriteLine(boardingCheck.Name
13: + "'s ID is "
14: + boardingCheck.ID
15: + ", he/she wants to "
16: + boardingCheck.Destination
17: + " from "
18: + boardingCheck.Departure
19: + " by the plane "
20: + boardingCheck.FlightNumber
21: + ", his/her position is "
22: + boardingCheck.PositionNumber
23: + ".");
24: }
25: }
26:
27: Console.ReadLine();
28: }