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-18 09:37:14

C#枚举类型

2009-08-25 15:59:28

C#串口操作

2009-08-19 16:42:41

C#如何使用XML

2009-08-13 13:29:04

C#结构体使用

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-09-08 14:54:40

C# listBox控

2009-08-28 16:31:21

C# treeview

2009-08-17 18:34:50

C# ChangeCo

2009-08-14 17:45:52

C# ArrayLis

2009-08-07 17:25:37

C# SortedLi

2009-09-02 10:58:02

C#动态数组

2009-08-25 17:59:49

C#入门

2009-08-20 18:37:52

委托C#异步委托

2009-08-17 13:56:29

C#进度条的使用

2009-08-26 09:54:45

C#打印预览C#打印
点赞
收藏

51CTO技术栈公众号