成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C# AttributeUsage的使用淺析

開發 后端
c# AttributeUsage的使用在我們開發中是十分常見的,那么我們了解c# AttributeUsage的基本情況從何入手呢?那么本文就向你詳細介紹相關的內容。

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

C# AttributeUsage的使用要明白:

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

◆ValidOn 
 
通過這個屬性,我們能夠定義定制特性應該在何種程序實體前放置。一個屬性可以被放置的所有程序實體在AttributeTargets enumerator中列出。通過OR操作我們可以把若干個AttributeTargets值組合起來。

◆AllowMultiple 
 
這個屬性標記了我們的定制特性能否被重復放置在同一個程序實體前多次。

◆Inherited 
 
我們可以使用這個屬性來控制定制特性的繼承規則。它標記了我們的特性能否被繼承。

C# AttributeUsage的使用實例:

下面讓我們來做一些實際的東西。我們將會在剛才的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.  }   

先讓我們來看一下AttributeTargets.Class。它規定了Help特性只能被放在class的前面。這也就意味著下面的代碼將會產生錯誤: 

  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.  }   

編譯器報告錯誤如下:

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

我們可以使用AttributeTargets.All來允許Help特性被放置在任何程序實體前。可能的值是:

  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。它規定了特性不能被重復放置多次。

  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. }  

它產生了一個編譯期錯誤。 

  1. AnyClass.cs: Duplicate 'Help' attribute 

Ok,現在我們來討論一下***的這個屬性。Inherited, 表明當特性被放置在一個基類上時,它能否被派生類所繼承。

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

C# AttributeUsage的使用會有四種可能的組合:

  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)(稍后我們會看到如何在運行期查詢一個類的特性)Derive類,我們將會發現Help特性并不存在,因為inherited屬性被設置為false。

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

和***種情況相同,因為inherited也被設置為false。

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

為了解釋第三種和第四種情況,我們先來給派生類添加點代碼: 

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

現在我們來查詢一下Help特性,我們只能得到派生類的屬性,因為inherited被設置為true,但是AllowMultiple卻被設置為false。因此基類的Help特性被派生類Help特性覆蓋了。

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

在這里,我們將會發現派生類既有基類的Help特性,也有自己的Help特性,因為AllowMultiple被設置為true。

C# AttributeUsage的相關內容就向你介紹到這里,希望對你了解和掌握C# AttributeUsage的使用有所幫助。

【編輯推薦】

  1. RSA實現C# 加密詳解
  2. 詳解TripleDES實現C# 加密操作
  3. 淺析C# WinForm控件開發前期準備
  4. 詳解C# WinForm自定義控件的使用和調試
  5. C# Attribute的概念與使用淺析
責任編輯:仲衡 來源: 百度空間
相關推薦

2009-08-14 15:23:10

C#使用ErrorPr

2009-09-04 15:45:29

C#緩存流

2009-08-25 15:59:28

C#串口操作

2009-08-18 09:37:14

C#枚舉類型

2009-08-13 13:29:04

C#結構體使用

2009-08-19 16:42:41

C#如何使用XML

2009-08-25 16:29:33

C#使用sqlserv

2009-09-11 11:16:53

C# Attribut

2009-08-26 13:36:33

C#打印控件

2009-08-13 14:56:46

C#的結構體使用

2009-08-14 17:45:52

C# ArrayLis

2009-08-17 18:34:50

C# ChangeCo

2009-08-07 17:25:37

C# SortedLi

2009-09-08 14:54:40

C# listBox控

2009-08-28 16:31:21

C# treeview

2009-08-25 17:59:49

C#入門

2009-09-02 10:58:02

C#動態數組

2009-08-20 18:37:52

委托C#異步委托

2009-08-17 13:56:29

C#進度條的使用

2009-08-10 14:43:03

C#函數Convert
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品爱久久久久久久 | 黄色成人av| 欧美爱爱视频 | 成人精品鲁一区一区二区 | 中文字幕成人 | 亚洲三区在线观看 | 国产精品高清在线 | 成年网站在线观看 | 久久久久久影院 | 国内自拍偷拍 | 99re热精品视频国产免费 | 亚洲小视频在线播放 | 国产在线一级片 | 九九九久久国产免费 | 色网站在线免费观看 | 国产在线一区二区 | 日韩在线不卡视频 | 一区二区中文字幕 | 欧美日韩a| 免费在线观看成年人视频 | 久久视频免费看 | 成人免费观看男女羞羞视频 | 国产成人精品一区二 | 亚洲精品视频一区 | 国产精品色 | 国产美女一区二区 | 91视频久久| 一级做a爰片性色毛片16 | 亚洲欧美日韩精品久久亚洲区 | 亚洲国产aⅴ成人精品无吗 国产精品永久在线观看 | 日韩欧美在线视频观看 | 狠狠操天天操 | 九九久久精品视频 | 久久r免费视频 | 在线观看成人av | 亚洲一区二区三区久久 | 精品无码久久久久久久动漫 | 懂色tv| 国产精品视频一二三区 | 亚欧精品一区 | 亚洲一区久久 |