自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C# AttributeUsage的使用淺析

開(kāi)發(fā) 后端
c# AttributeUsage的使用在我們開(kāi)發(fā)中是十分常見(jiàn)的,那么我們了解c# AttributeUsage的基本情況從何入手呢?那么本文就向你詳細(xì)介紹相關(guān)的內(nèi)容。

C# AttributeUsage的使用是如何的呢?首先讓我們來(lái)了解一下什么是AttributeUsage類它是另外一個(gè)預(yù)定義特性類,AttributeUsage類的作用就是幫助我們控制定制特性的使用。其實(shí)AttributeUsage類就是描述了一個(gè)定制特性如和被使用。

C# AttributeUsage的使用要明白:

AttributeUsage有三個(gè)屬性,我們可以把它放置在定制屬性前面。***個(gè)屬性是:

◆ValidOn 
 
通過(guò)這個(gè)屬性,我們能夠定義定制特性應(yīng)該在何種程序?qū)嶓w前放置。一個(gè)屬性可以被放置的所有程序?qū)嶓w在AttributeTargets enumerator中列出。通過(guò)OR操作我們可以把若干個(gè)AttributeTargets值組合起來(lái)。

◆AllowMultiple 
 
這個(gè)屬性標(biāo)記了我們的定制特性能否被重復(fù)放置在同一個(gè)程序?qū)嶓w前多次。

◆Inherited 
 
我們可以使用這個(gè)屬性來(lái)控制定制特性的繼承規(guī)則。它標(biāo)記了我們的特性能否被繼承。

C# AttributeUsage的使用實(shí)例:

下面讓我們來(lái)做一些實(shí)際的東西。我們將會(huì)在剛才的Help特性前放置AttributeUsage特性以期待在它的幫助下控制Help特性的使用。

  1. using System;   
  2. [AttributeUsage(AttributeTargets.Class), AllowMultiple = false,   
  3.  Inherited = false ]   
  4. public class HelpAttribute : Attribute   
  5. {   
  6.  public HelpAttribute(String Description_in)   
  7.  {   
  8.  this.description = Description_in;   
  9.  }   
  10.  protected String description;   
  11.  public String Description   
  12.  {   
  13.  get   
  14.  {   
  15.  return this.description;   
  16.  }   
  17.  }   

先讓我們來(lái)看一下AttributeTargets.Class。它規(guī)定了Help特性只能被放在class的前面。這也就意味著下面的代碼將會(huì)產(chǎn)生錯(cuò)誤: 

  1. [Help("this is a do-nothing class")]   
  2. public class AnyClass   
  3. {   
  4.  [Help("this is a do-nothing method")] //error   
  5.  public void AnyMethod()   
  6.  {   
  7.  }   

編譯器報(bào)告錯(cuò)誤如下:

  1. AnyClass.cs: Attribute 'Help' is not valid on this declaration type.  
  2.  
  3. It is valid on 'class' declarations only.  

我們可以使用AttributeTargets.All來(lái)允許Help特性被放置在任何程序?qū)嶓w前??赡艿闹凳牵?/P>

  1. Assembly,   
  2. Module,   
  3. Class,   
  4. Struct,   
  5. Enum,   
  6. Constructor,   
  7. Method,   
  8. Property,   
  9. Field,   
  10. Event,   
  11. Interface,   
  12. Parameter,   
  13. Delegate,   
  14. All = Assembly | Module | Class |   
  15. Struct | Enum | Constructor |   
  16. Method | Property | Field | Event |   
  17. Interface | Parameter | Delegate,   
  18. ClassMembers = Class | Struct | Enum |  
  19.  Constructor | Method | Property | Field |  
  20.  Event | Delegate | Interface ) 

下面考慮一下AllowMultiple = false。它規(guī)定了特性不能被重復(fù)放置多次。

  1. [Help("this is a do-nothing class")]   
  2. [Help("it contains a do-nothing method")]   
  3. public class AnyClass   
  4. {   
  5.  [Help("this is a do-nothing method")] //error   
  6.  public void AnyMethod()   
  7.  {   
  8.  }   
  9. }  

它產(chǎn)生了一個(gè)編譯期錯(cuò)誤。 

  1. AnyClass.cs: Duplicate 'Help' attribute 

Ok,現(xiàn)在我們來(lái)討論一下***的這個(gè)屬性。Inherited, 表明當(dāng)特性被放置在一個(gè)基類上時(shí),它能否被派生類所繼承。

  1.  [Help("BaseClass")]   
  2. public class Base   
  3. {   
  4. }   
  5.    
  6. public class Derive : Base   
  7. {   

C# AttributeUsage的使用會(huì)有四種可能的組合:

  1. [AttributeUsage(AttributeTargets.Class,  
  2.  AllowMultiple = false, Inherited = false ]   
  3. [AttributeUsage(AttributeTargets.Class,   
  4. AllowMultiple = true, Inherited = false ]   
  5. [AttributeUsage(AttributeTargets.Class,   
  6. AllowMultiple = false, Inherited = true ]   
  7. [AttributeUsage(AttributeTargets.Class,   
  8. AllowMultiple = true, Inherited = true ] 

C# AttributeUsage的使用***種情況:

如果我們查詢(Query)(稍后我們會(huì)看到如何在運(yùn)行期查詢一個(gè)類的特性)Derive類,我們將會(huì)發(fā)現(xiàn)Help特性并不存在,因?yàn)閕nherited屬性被設(shè)置為false。

C# AttributeUsage的使用第二種情況:

和***種情況相同,因?yàn)閕nherited也被設(shè)置為false。

C# AttributeUsage的使用第三種情況:

為了解釋第三種和第四種情況,我們先來(lái)給派生類添加點(diǎn)代碼: 

  1. [Help("BaseClass")]   
  2. public class Base   
  3. {   
  4. }   
  5. [Help("DeriveClass")]   
  6. public class Derive : Base   
  7. {   

現(xiàn)在我們來(lái)查詢一下Help特性,我們只能得到派生類的屬性,因?yàn)閕nherited被設(shè)置為true,但是AllowMultiple卻被設(shè)置為false。因此基類的Help特性被派生類Help特性覆蓋了。

C# AttributeUsage的使用第四種情況:

在這里,我們將會(huì)發(fā)現(xiàn)派生類既有基類的Help特性,也有自己的Help特性,因?yàn)锳llowMultiple被設(shè)置為true。

C# AttributeUsage的相關(guān)內(nèi)容就向你介紹到這里,希望對(duì)你了解和掌握C# AttributeUsage的使用有所幫助。

【編輯推薦】

  1. RSA實(shí)現(xiàn)C# 加密詳解
  2. 詳解TripleDES實(shí)現(xiàn)C# 加密操作
  3. 淺析C# WinForm控件開(kāi)發(fā)前期準(zhǔn)備
  4. 詳解C# WinForm自定義控件的使用和調(diào)試
  5. C# Attribute的概念與使用淺析
責(zé)任編輯:仲衡 來(lái)源: 百度空間
相關(guān)推薦

2009-08-14 15:23:10

C#使用ErrorPr

2009-08-25 15:59:28

C#串口操作

2009-08-18 09:37:14

C#枚舉類型

2009-09-04 15:45:29

C#緩存流

2009-08-25 16:29:33

C#使用sqlserv

2009-08-19 16:42:41

C#如何使用XML

2009-08-13 13:29:04

C#結(jié)構(gòu)體使用

2009-08-13 14:56:46

C#的結(jié)構(gòu)體使用

2009-08-26 13:36:33

C#打印控件

2009-09-11 11:16:53

C# Attribut

2009-08-07 17:25:37

C# SortedLi

2009-08-28 16:31:21

C# treeview

2009-09-08 14:54:40

C# listBox控

2009-08-14 17:45:52

C# ArrayLis

2009-08-17 18:34:50

C# ChangeCo

2009-09-02 10:58:02

C#動(dòng)態(tài)數(shù)組

2009-08-25 17:59:49

C#入門(mén)

2009-08-17 13:56:29

C#進(jìn)度條的使用

2009-08-20 18:37:52

委托C#異步委托

2009-08-20 16:15:19

C# 匿名方法
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)