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 = , 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=

Attribute的理解和认识的更多相关文章

  1. DOM中 property 和 attribute 详解

    被问到 property 和 attribute 的区别,想来也是要好好看一下. 一.基本概念区别 其实Attribute和Property这两个单词,翻译出来都是“属性”,<js高级程序设计& ...

  2. 属性attribute和property的区别

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...

  3. Python2.2-原理之类型和运算

    此节来自于<Python学习手册第四版>第二部分 一.Python对象类型(第4章) 1. Python可以分解成模块.语句.表达式以及对象:1.程序由模块构成:2.模块包含语句:3.语句 ...

  4. 着色器(Shader)

    着色器(Shader) 顶点着色器(Vertex shader) 片段着色器(Fragment shader) 几何着色器(Geometry Shader) 提供通用计算能力的着色器(Compute ...

  5. 阶段02JavaWeb基础day02&03JavaScript

    javascript知识体系 ECMAScript javascript与html结合方式 内部: <script type="text/javaScript">*** ...

  6. jQuery .attr() vs. .prop()

    Property vs. Attribute 在开始正式比较prop()和attr()两个jQuery方法之前,我们有必要先弄清一下Property和Attribute两个单词的意思.在中文里面,它们 ...

  7. iOS - keychain 详解及变化

    keychain介绍 iOS keychain 是一个相对独立的空间,保存到keychain钥匙串中的信息不会因为卸载/重装app而丢失, .相对于NSUserDefaults.plist文件保存等一 ...

  8. LDAP学习小结【仅原理和基础篇】

    此篇文章花费了好几个晚上,大部分是软件翻译的英文文档,加上自己的理解所写,希望学习者能尊重每个人的努力. 我有句话想送给每个看我文章的人: 慢就是快,快就是慢!!! 另外更希望更多人能从认真从原理学习 ...

  9. django优化--ORM优缺点

    谈Django绕不开ORM ORM : ORM概念,ORM特点,ORM 的优点,ORM 的缺点 orm : 对象关系映射 (Object Relational Mapping) ,用于实现面向对象编程 ...

随机推荐

  1. 【IOS开发】UItextfield输入电话号码,自动调整格式

    UItextfield中实现输入电话号码,自动按位置在加“—”效果.效果图如下. 核心代码: -(BOOL)textField:(UITextField *)textField shouldChang ...

  2. json序列化NHibernate的实体

    在使用nhibernate时,想将实体对象序列化成json字符串,然后打印在日志中. 序列化时会出现问题,应该是因为这个实体被hibernate管理的原因.具体原因没有分析. 解决方案:为实体创建一个 ...

  3. asp.net 的一个简单进度条功能

    我们先看下效果 我点击了按钮后他会显示进度页面,进度完成后,进度条消失,其实也是比较简单的了. 我们需要一个进度条代码文件ProgressBar.htm(注意:是没有head这些标签的) <sc ...

  4. [原]MobileSubstrate 工作流程

    [附-腾讯安全管家替换 MobileSubstrate 的流程] com.qq.mqqsecure.deb-postinst--->QSCommand--->QSTempRunner

  5. IOS开发之路三(XML解析之GDataXML的使用)

    最近再做一个项目需要用到xml的解析.今天查了一些资料自己做了一个小demo.纯OC没有界面.. 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用 ...

  6. TortoiseSVN使用方法 安装和配置

    TortoiseSVN使用方法   安装和配置 TortoiseSVN的下载地址为 http://tortoisesvn.net/downloads.html 有32位和64位的版本,一定要根据自己的 ...

  7. markdownpad2注册及样式调整

    pro版密钥 邮箱: Soar360@live.com key: GBPduHjWfJU1mZqcPM3BikjYKF6xKhlKIys3i1MU2eJHqWGImDHzWdD6xhMNLGVpbP2 ...

  8. Pig性能优化

    Pig性能优化 1. 尽早去除无用的数据 MapReduce Job的很大一部分开销在于磁盘IO和数据的网络传输,如果能尽早的去除无用的数据,减少数据量,会提升Pig的性能. 1). 尽早的使用Fil ...

  9. 解决phpmailer可以在windows下面发送成功, 在linux下面失败的问题

    谢天谢地...差点因为在linux下面phpmailer发送邮件失败转到了window+IIS... Godaddy的linux服务器无法用phpmailer发送(我用的是网易的邮箱服务器...虽然现 ...

  10. SQL Server 2014新特性:五个关键点带你了解Excel下的Data Explorer

    SQL Server 2014新特性:五个关键点带你了解Excel下的Data Explorer Data Explorer是即将发布的SQL Server 2014里的一个新特性,借助这个特性讲使企 ...