? 首先要說的是,可能一些剛接觸C#的朋友常常容易把屬性(Property)跟特性(Attribute)弄混淆,其實這是兩種不同的東西。屬性就是面向對象思想里所說的封裝在類里面的數據字段,其形式為:
?
????{
????????public?String?Name?{?get;?set;?}
????????public?int?Age?{?get;?set;?}
????????public?int?Gender?{?get;?set;?}
????}
?
?
在HumanBase這個類里出現的字段都叫屬性(Property),而C#特性(Attribute)又是怎樣的呢?
?
????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是不是可序列化的,可以通過:
?
????????{
????????????Response.Write(typeof(HumanBase).IsSerializable);
????????}
?
?
??? 拿到了登機牌,就意味著你可以合法地登機起飛了。但此時你還不知道你要坐的飛機停在哪里,不用擔心,地勤人員會開車送你過去,但是他怎么知道你是哪趟航班的呢?顯然還是通過你手中的登機牌。所以,特性最大的特點就是自描述。
既然是起到描述的作用,那目的就是在于限定。就好比地勤不會把你隨便拉到一架飛機跟前就扔上去了事,因為標簽上的說明信息就是起到限定的作用,限定了目的地、乘客和航班,任何差錯都被視為異常。如果前面的HumanBase不加上Serializable特性就不能在網絡上傳輸。
我們在順帶來介紹一下方法特性,先給HumanProperty加上一個Run方法:
?


????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?helath;
????????}
????}
?
?
??????? 只要是個四肢健全、身體健康的人就可以跑步,那這么說,跑步就是有前提條件的,至少是四肢健全,身體健康。由此可見,殘疾人和老年人如果跑步就會出問題。假設一個HumanBase的對象代表的是一位耄耋老人,如果讓他當劉翔的陪練,那就直接光榮了。如何避免這樣的情況呢,我們可以在Run方法中加一段邏輯代碼,先判斷Age大小,如果小于2或大于60直接拋異常,但是2-60歲之間也得用Switch來分年齡階段地判斷speed參數是否合適,那么邏輯就相當臃腫。簡而言之,如何用特性表示一個方法不能被使用呢?OK, here we go:
?


????????public?virtual?void?Run(int?speed)
????????{?
???????????//?Running?is?good?for?helath;
????????}
?
?
??? 上面大致介紹了一下特性的使用與作用,接下來我們要向大家展示的是如何通過自定義特性來提高程序的靈活性,如果特性機制僅僅能使用.NET提供的那幾種特性,不就太不過癮了么。
首先,特性也是類。不同于其它類的是,特性都必須繼承自System.Attribute類,否則編譯器如何知道誰是特性誰是普通類呢。當編譯器檢測到一個類是特性的時候,它會識別出其中的信息并存放在元數據當中,僅此而已,編譯器并不關心特性說了些什么,特性也不會對編譯器起到任何作用,正如航空公司并不關心每個箱子要去哪里,只有箱子的主人和搬運工才會去關心這些細節。假設我們現在就是航空公司的管理人員,需要設計出前面提到的登機牌,那么很簡單,我們先看看最主要的信息有哪些:
?


????{
????????public?string?ID
????????{
????????????get;
????????????private?set;
????????}
????????public?string?Name
????????{
????????????get;
????????????private?set;
????????}
????????public?int?FlightNumber
????????{
????????????get;
????????????private?set;
????????}
????????public?int?PositionNumber
????????{
????????????get;
????????????private?set;
????????}
????????public?string?Departure
????????{
????????????get;
????????????private?set;
????????}
????????public?string?Destination
????????{
????????????get;
????????????private?set;
????????}
????}
?
?
??? 我們簡單列舉這些屬性作為航空公司登機牌上的信息,用法和前面的一樣,貼到HumanBase上就行了,說明此人具備登機資格。這里要簡單提一下,你可能已經注意到了,在使用BoardingCheckAttribute的時候已經把Attribute省略掉了,不用擔心,這樣做是對的,因為編譯器默認會自己加上然后查找這個屬性類的。哦,等一下,我突然想起來他該登哪架飛機呢?顯然,在這種需求下,我們的特性還沒有起到應有的作用,我們還的做點兒工作,否則乘客面對一張空白的機票一定會很迷茫。
?
?? 于是,我們必須給這個C#特性加上構造函數,因為它不僅僅表示登機的資格,還必須包含一些必要的信息才行:
?


????????{
????????????this.ID?=?id;
????????????this.Name?=?name;
????????????this.FlightNumber?=?flightNumber;
????????????this.PositionNumber?=?positionNumber;
????????????this.Departure?=departure;
????????????this.Destination?=?destination;
????????}
?
?OK,我們的乘客就可以拿到一張正式的登機牌登機了,have a good flight!
?


????public?class?HumanPropertyBase
????{
????????public?string?Name?{?get;?set;?}
????????public?int?Age?{?get;?set;?}
????????public?int?Gender?{?get;?set;?}
????}
????public?class?BoardingCheckAttribute:System.Attribute
????{
????????public?BoardingCheckAttribute(string?id,string?name,int?flightNumber,int?positionNumber,string?departure,string?destination)
????????{
????????????this.ID?=?id;
????????????this.Name?=?name;
????????????this.FlightNumber?=?flightNumber;
????????????this.PositionNumber?=?positionNumber;
????????????this.Departure?=departure;
????????????this.Destination?=?destination;
????????}
????????public?string?ID
????????{
????????????get;
????????????private?set;
????????}
????????public?string?Name
????????{
????????????get;
????????????private?set;
????????}
????????public?int?FlightNumber
????????{
????????????get;
????????????private?set;
????????}
????????public?int?PositionNumber
????????{
????????????get;
????????????private?set;
????????}
????????public?string?Departure
????????{
????????????get;
????????????private?set;
????????}
????????public?string?Destination
????????{
????????????get;
????????????private?set;
????????}
????}
????public?partial?class?WebForm1?:?System.Web.UI.Page
????{
????????protected?void?Page_Load(object?sender,?EventArgs?e)
????????{
????????????BoardingCheckAttribute?boradingCheck?=?null;
????????????object[]?customAttributes?=?typeof(HumanPropertyBase).GetCustomAttributes(true);
????????????foreach?(var?attribute?in?customAttributes)
????????????{
????????????????if?(attribute?is?BoardingCheckAttribute)
????????????????{
????????????????????boradingCheck?=?attribute?as?BoardingCheckAttribute;
????????????????????Response.Write(boradingCheck.Name+"'s?ID?is?"
?????????????????????+boradingCheck.ID+",he/she?wants?to?"+
?????????????????????boradingCheck.Destination+"?from?"+
?????????????????????boradingCheck.Departure+"?by?the?plane?"
?????????????????????+boradingCheck.FlightNumber+
?????????????????????",his/her?position?is?"+boradingCheck.PositionNumber+"."
?????????????????????);
????????????????}
????????????}
????????}
????}
?
?