• Attribute的理解和认识


    1什么是Attribute?

    在网上看到这样一段定义

    MADN的定义为:公共语言运行时允许添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型、字段、方法和属性等。Attributes和Microsoft .NET Framework文件的元数据(metadata)保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为。

    我总结一下,包括以下几个核心的概念:

    1. Attribute是类;
    2. Attribute功能上类似描述、注释;
    3. 作用的对象是程序的元素,包括了class,method,property,struct等等;
    4. Attribute与.NET的元数据保存在一起,作为一种描述程序的元数据而存在。

     

    2为什么需要Attribute?

    Attribute是一种元数据描述,这种描述能够做到:

    1. 向.NET运行时,描述程序自身;
    2. 影响程序的行为。

    因此,有着多种用途,例如序列化;安全;防止编译器优化,而方便调试;等等。

     

    3如何使用Attribute?

    1. 所有自定义的Attribute一定要继承自System.Attribute;
    2. 创建Attribute时要注意,自定义Attribute的名称一定要以Attribute做后缀,而使用时,不用带上后缀。例如:CustomAttribute,IDefineAttribute,YouMadeThisAttribute,在使用他们作为程序元数据时,则使用Custom,IDefine,YouMadeThis。

     

    4附上一段MSDN上的描述

    The Attribute class associates predefined system information or user-defined custom information with a target element. A target element can be an assembly, class, constructor, delegate, enum, event, field, interface, method, portable executable file module, parameter, property, return value, struct, or another attribute.

    Information provided by an attribute is also known as metadata. Metadata can be examined at run time by your application to control how your program processes data, or before run time by external tools to control how your application itself is processed or maintained. For example, the .NET Framework predefines and uses attribute types to control run-time behavior, and some programming languages use attribute types to represent language features not directly supported by the .NET Framework common type system.

    All attribute types derive directly or indirectly from the Attribute class. Attributes can be applied to any target element; multiple attributes can be applied to the same target element; and attributes can be inherited by an element derived from a target element. Use the AttributeTargets class to specify the target element to which the attribute is applied.

    The Attribute class provides convenient methods to retrieve and test custom attributes. For more information about using attributes, see Applying Attributes and Extending Metadata Using Attributes.

     

    5示例程序

     1 using System;
     2 
     3 namespace ConsoleApplication1
     4 {
     5     [AttributeUsage(AttributeTargets.Class)]
     6     public class welearnattributeAttribute : Attribute
     7     {
     8         public string Version { get; set; }
     9         public string Description { get; set; }
    10         public int Modify { get; set; }
    11     }
    12 
    13     [AttributeUsage(AttributeTargets.Method)]
    14     public class methodattriAttribute : Attribute
    15     {
    16         public string Name { get; set; }
    17     }
    18 
    19     [welearnattribute(Description = "testclassdescription", Modify = 10, Version = "1.0.0.2")]
    20     public class testclass
    21     {
    22         [methodattri(Name = "warnet's method")]
    23         public void Method()
    24         {
    25             Console.WriteLine("From Method");
    26         }
    27     }
    28 
    29     public class TestAtrributeClass
    30     {
    31         /*
    32          * output:
    33          * testclassdescription
    34          * 10
    35          * 1.0.0.2
    36          * warnet's method
    37          */
    38         public static void Solution()
    39         {
    40             var data = typeof(testclass);
    41             var attributes = (welearnattributeAttribute)Attribute.GetCustomAttribute(data, typeof(welearnattributeAttribute));
    42             Console.WriteLine(attributes.Description);
    43             Console.WriteLine(attributes.Modify);
    44             Console.WriteLine(attributes.Version);
    45             var mdattri = (methodattriAttribute)Attribute.GetCustomAttribute(data.GetMethod("Method"), typeof(methodattriAttribute));
    46             Console.WriteLine(mdattri.Name);
    47         }
    48     }
    49 }

    6参考链接

    1. http://www.cnblogs.com/luckdv/articles/1682488.html
    2. http://www.cnblogs.com/hyddd/archive/2009/07/20/1526777.html
    3. https://msdn.microsoft.com/zh-cn/library/system.attribute(v=vs.110).aspx?query=
  • 相关阅读:
    iOS开发数据库篇—SQLite的应用
    iOS开发数据库篇—SQL代码应用示例
    iOS开发数据库篇—SQL
    iOS开发数据库篇—SQLite简单介绍
    iOS开发网络篇—NSURLConnection基本使用
    iOS开发网络篇—数据安全
    iOS开发网络篇—GET请求和POST请求
    WordPress主题开发:开启文章缩略图功能
    WordPress主题开发:开启feed功能
    WordPress主题开发:循环代码
  • 原文地址:https://www.cnblogs.com/warnet/p/4891641.html
Copyright © 2020-2023  润新知