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