C#特性

來源:互聯網
上載者:User

系列文章索引:《白話C#

首先要說的是,可能一些剛接觸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: 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.