温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C# 中AttributeUsage如何使用

发布时间:2021-07-07 17:50:15 来源:亿速云 阅读:374 作者:Leah 栏目:编程语言

本篇文章给大家分享的是有关C# 中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如何使用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI