C# 特性(Attribute)入門(二)

來源:互聯網
上載者:User
定義或控制特性的使用
  
  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;
   }
   }
  }
  先讓我們來看一下AttributeTargets.Class。它規定了Help特性只能被放在class的前面。這也就意味著下面的代碼將會產生錯誤:
  
  [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特性,我們只能得到衍生類別的屬性,因為inherited被設定為true,但是AllowMultiple卻被設定為false。因此基類的Help特性被衍生類別Help特性覆蓋了。
  
  
  
  第四種情況:
  
  在這裡,我們將會發現衍生類別既有基類的Help特性,也有自己的Help特性,因為AllowMultiple被設定為true。
相關文章

聯繫我們

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