• (转)C# 特性(Attribute)详细介绍


    本文转载自:http://www.cnblogs.com/luckdv/articles/1682488.html

    1、什么是Atrribute 
    首先,我们肯定Attribute是一个类,下面是msdn文档对它的描述: 
    公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型、字段、方法和属性等。Attributes和Microsoft .NET Framework文件的元数据保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为。

    在.NET中,Attribute被用来处理多种问题,比如序列化、程序的安全特征、防止即时编译器对程序代码进行优化从而代码容易调试等等。下面,我们先来看几个在.NET中标准的属性的使用,稍后我们再回过头来讨论Attribute这个类本身。(文中的代码使用C#编写,但同样适用所有基于.NET的所有语言)

    3、Attribute类 
    除了.NET提供的那些Attribute派生类之外,我们可以自定义我们自己的Attribute,所有自定义的Attribute必须从Attribute类派生。现在我们来看一下Attribute 类的细节:

    protected Attribute(): 保护的构造器,只能被Attribute的派生类调用。

    三个静态方法:

    static Attribute GetCustomAttribute():这个方法有8种重载的版本,它被用来取出施加在类成员上指定类型的Attribute。

    static Attribute[] GetCustomAttributes(): 这个方法有16种重载版本,用来取出施加在类成员上指定类型的Attribute数组。

    static bool IsDefined():由八种重载版本,看是否指定类型的定制attribute被施加到类的成员上面。

    实例方法:

    bool IsDefaultAttribute(): 如果Attribute的值是默认的值,那么返回true。

    bool Match():表明这个Attribute实例是否等于一个指定的对象。

    公共属性: TypeId: 得到一个唯一的标识,这个标识被用来区分同一个Attribute的不同实例。

    我们简单地介绍了Attribute类的方法和属性,还有一些是从object继承来的。这里就不列出来了。

    下面介绍如何自定义一个Attribute: 自定义一个Attribute并不需要特别的知识,其实就和编写一个类差不多。自定义的Attribute必须直接或者间接地从Attribute这个类派生,如:

    public MyCustomAttribute : Attribute { ... }

    这里需要指出的是Attribute的命名规范,也就是你的Attribute的类名+"Attribute",当你的Attribute施加到一个程序的元素上的时候,编译器先查找你的Attribute的定义,如果没有找到,那么它就会查找“Attribute名称"+Attribute的定义。如果都没有找到,那么编译器就报错。

    4、定义或控制特性的使用

    对于一个自定义的Attribute,你可以通过AttributeUsage的Attribute来限定你的Attribute 所施加的元素的类型。代码形式如下: 
    [AttriubteUsage(参数设置)] public 自定义Attribute : Attribute { ... }

    作为参数的AttributeTarges的值允许通过“或”操作来进行多个值得组合,如果你没有指定参数,那么默认参数就是All 。 AttributeUsage除了继承Attribute 的方法和属性之外,还定义了以下三个属性:

    AllowMultiple: 读取或者设置这个属性,表示是否可以对一个程序元素施加多个Attribute 。

    Inherited:读取或者设置这个属性,表示是否施加的Attribute 可以被派生类继承或者重载。

    ValidOn: 读取或者设置这个属性,指明Attribute 可以被施加的元素的类型。

    下面让我们来做一些实际的东西。我们将会在Help特性前放置AttributeUsage特性以期待在它的帮助下控制Help特性的使用。

    隐藏行号 复制代码 ?这是一段程序代码。
    [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;
            }
        }
    }

    代码讨论

    • 属性 AttributeUsage 指定该属性可以应用于的语言元素。
    • 属性类是从 System.Attribute 派生的公共类,至少有一个公共构造函数。
    • 属性类有两种类型的参数:
      •       “定位参数”,每次使用属性时都必须指定这些参数。定位参数被指定为属性类的构造函数参数。在上面的示例中,url 便是一个定位参数。
      •      “命名参数”,可选。如果使用属性时指定了命名参数,则必须使用参数的名称。通过包含非静态字段或属性来定义命名参数。在上面的示例中,Topic 便是一个命名参数。

    先让我们来看一下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
    {
    
    }
     

    这里会有四种可能的组合:

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

    第一种情况:

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

    第二种情况:

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

    第三种情况:

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

    [Help("BaseClass")]
    public class Base
    {
    
    }
    [Help("DeriveClass")]
    public class Derive : Base
    {
    
    }

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

    第四种情况:

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

            • 通过反射访问属性

    属性与程序元素关联后,可以使用反射查询属性存在及其值。查询属性的主要反射方法包含在 System.Reflection.MemberInfo 类(GetCustomAttributes 方法族)中。下面的示例演示使用反射获取对属性的访问的基本方法:

    System.Reflection.MemberInfo info = typeof(Help);
    object[] attributes = info.GetCustomAttributes(true);
    for (int i = 0; i < attributes.Length; i++)
    {
        System.Console.WriteLine(attributes[i]);
    }
  • 相关阅读:
    《Java技术》第七次作业计科1501赵健宇
    《Java技术》第六次作业计科1501赵健宇
    《Java技术》第五次作业计科1501赵健宇
    《Java技术》第四次作业计科1501赵健宇
    《Java技术》第三次作业计科1501赵健宇
    整站灰度CSS 代码
    HTML中input输入框禁止复制粘贴剪切自动完成
    无法嵌入互操作类型“ApplicationClass”。请改用适用的接口
    添加右键VSCode打开
    async & await 异步编程小示例,一看就懂
  • 原文地址:https://www.cnblogs.com/wpcnblog/p/4389422.html
Copyright © 2020-2023  润新知