C# AttributeUsage的使用是如何的呢?首先让我们来了解一下什么是AttributeUsage类它是另外一个预定义特性类,AttributeUsage类的作用就是帮助我们控制定制特性的使用。其实AttributeUsage类就是描述了一个定制特性如和被使用。

  C# AttributeUsage的使用要明白:

  AttributeUsage有三个属性,我们可以把它放置在定制属性前面。第一个属性是:

  ◆ValidOn

  通过这个属性,我们能够定义定制特性应该在何种程序实体前放置。一个属性可以被放置的所有程序实体在AttributeTargets enumerator中列出。通过OR操作我们可以把若干个AttributeTargets值组合起来。

  ◆AllowMultiple

  这个属性标记了我们的定制特性能否被重复放置在同一个程序实体前多次。

  ◆Inherited

  我们可以使用这个属性来控制定制特性的继承规则。它标记了我们的特性能否被继承。

  C# AttributeUsage的使用实例:

  下面让我们来做一些实际的东西。我们将会在刚才的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

  {

  }

  C# AttributeUsage的使用会有四种可能的组合:

  [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 ]

  C# AttributeUsage的使用第一种情况:

  如果我们查询(Query)(稍后我们会看到如何在运行期查询一个类的特性)Derive类,我们将会发现Help特性并不存在,因为inherited属性被设置为false。

  C# AttributeUsage的使用第二种情况:

  和第一种情况相同,因为inherited也被设置为false。

  C# AttributeUsage的使用第三种情况:

  为了解释第三种和第四种情况,我们先来给派生类添加点代码:

  [Help("BaseClass")]

  public class Base

  {

  }

  [Help("DeriveClass")]

  public class Derive : Base

  {

  }

  现在我们来查询一下Help特性,我们只能得到派生类的属性,因为inherited被设置为true,但是AllowMultiple却被设置为false。因此基类的Help特性被派生类Help特性覆盖了。

  C# AttributeUsage的使用第四种情况:

  在这里,我们将会发现派生类既有基类的Help特性,也有自己的Help特性,因为AllowMultiple被设置为true。

  C# AttributeUsage的相关内容就向你介绍到这里,希望对你了解和掌握C# AttributeUsage的使用有所帮助。

C# AttributeUsage的使用相关推荐

  1. 特性入门AttributeUsage

    msdn:ms-help://MS.MSDNQTR.2003FEB.2052/csspec/html/vclrfcsharpspec_17_1_1.htm AttributeUsage 属性(第 17 ...

  2. 介绍属性与自定义属性、AttributeUsage

    介绍属性                属性为访问自定义类型的注释信息提供通用的访问方式.注释信息是随意的,换句话说,这种信息不是语言自身固有的,而是由你自己能够想象到的任何信息.你能使用属性(att ...

  3. Attribute特性3——自定义特性AttributeUsage

    Attribute特性3--自定义特性AttributeUsage AttributeUsage 预定义特性 AttributeUsage 描述了如何使用一个自定义特性类.它规定了特性可应用到的项目的 ...

  4. AttributeUsage

    AttributeUsage [AttributeUsage] System.AttributeUsage声明一个Attribute的使用范围与使用原则. AllowMultiple 和 Inheri ...

  5. (C#学习)Attribute:AttributeUsage和反射

    Attribute(特性) Attribute类将预定义的系统信息或用户定义的自定义信息与目标元素关联起来. 目标元素可以是程序集.类.构造函数.委托.枚举.事件.字段.接口.方法.可移植的可执行文件 ...

  6. C#的自定义属性AttributeUsage用法

    [AttributeUsage( // AttributeUsage是.net定义的特性的内置特性说明,可以叫特性的元特性.AttributeTargets.All,// 应用到的 程序元素[必选], ...

  7. AttributeUsage属性

    除了定制attributes之外,可以使用Attributes属性定义如何使用这些属性.例如:<?xml:namespace prefix = o ns = "urn:schemas- ...

  8. 标签AttributeUsage 使用

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] 三个参数  第一个是标签作用范围可以是 ...

  9. C# AttributeUsage

    C# AttributeUsage的使用是如何的呢?首先让我们来了解一下什么是AttributeUsage类它是另外一个预定义特性类,AttributeUsage类的作用就是帮助我们控制定制特性的使用 ...

最新文章

  1. 如果可以,我想给这本书打十星!
  2. mysql的索引的区别_MYSQL索引区别
  3. PS常用快捷键就这些了,记住绘图事半功倍
  4. 软件测试组织与管理思维导图
  5. 会计记忆总结之四:会计凭证
  6. wxWidgets:wxSpinCtrl类用法
  7. java包命名规则名词_java中命名规范
  8. java年份换算_java中日期的换算处理
  9. java制作管理系统视频_阶段1:手把手快速做一个Java swing mysql学生信息管理系统附带完整源码及视频开发教程【猿来入此自营】...
  10. 预算分配Budget Allocation:两篇论文(二)
  11. python线程,进程,队列和缓存
  12. 【机器学习系列】EM算法第一讲:EM算法相关概述及收敛性证明
  13. c++采集声卡输出_windows上面捕获声卡数据
  14. mysql字段动态扩展_数据库动态扩展字段
  15. CorelDRAW常用工具之手绘工具
  16. Mysql计算同比环比(超详细)
  17. matlab四面体体积代码,求任意四面体体积公式
  18. 网站地图Sitemap怎么制作
  19. 迅为-4418开发板-驱动-PWM输出实验
  20. oracle时间字段加几小时

热门文章

  1. 怎么能快速赚钱(零成本快速赚钱方式分享)
  2. 小程序中where条件查询
  3. 光棍节脱单有望了,让你的爱被她看见丨钛空智慧星球推荐
  4. php写斐波那契数列,php 写斐波那契数列
  5. Web UI 自动化测试:如何使用隐私模式进行测试
  6. iTunes Connect 使用总结
  7. 这些Docker容器技术窍门你都知道吗?
  8. 均方误差越大越好_常用度量--MAE(平均绝对误差)和RMSE(均方根误差)
  9. Linux下查看图片——安装imgcat
  10. 谁说菜鸟不会数据分析电子书