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示例程序

 using System;

 namespace ConsoleApplication1
{
[AttributeUsage(AttributeTargets.Class)]
public class welearnattributeAttribute : Attribute
{
public string Version { get; set; }
public string Description { get; set; }
public int Modify { get; set; }
} [AttributeUsage(AttributeTargets.Method)]
public class methodattriAttribute : Attribute
{
public string Name { get; set; }
} [welearnattribute(Description = "testclassdescription", Modify = , Version = "1.0.0.2")]
public class testclass
{
[methodattri(Name = "warnet's method")]
public void Method()
{
Console.WriteLine("From Method");
}
} public class TestAtrributeClass
{
/*
* output:
* testclassdescription
* 10
* 1.0.0.2
* warnet's method
*/
public static void Solution()
{
var data = typeof(testclass);
var attributes = (welearnattributeAttribute)Attribute.GetCustomAttribute(data, typeof(welearnattributeAttribute));
Console.WriteLine(attributes.Description);
Console.WriteLine(attributes.Modify);
Console.WriteLine(attributes.Version);
var mdattri = (methodattriAttribute)Attribute.GetCustomAttribute(data.GetMethod("Method"), typeof(methodattriAttribute));
Console.WriteLine(mdattri.Name);
}
}
}

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. c# in deep 之LINQ读取xml(2)

    假如有以下xml文件 <?xml version="1.0" encoding="utf-8" ?><Date>  <Produc ...

  2. Content Negotiation(内容协商)

    Asp.Net Web API 2第十四课——Content Negotiation(内容协商)   前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http:// ...

  3. Newton迭代法-C++

    牛顿迭代法: 设定x*是方程f(x)=0的根,选取x0作为x*的近似值,过点(x0, f(x0))做曲线f(x)=0的切线L,L的方程y=f(x0)+f'(x0)(x-x0),求出L与x轴焦点的横坐标 ...

  4. Day3:T3DP T4堆排序

    QAQ因为T3的题解说的不是很清楚,但是blogs又不能按照自己想要的顺序排+略有一点强迫症 所以先挖个坑放着,过几天再补上

  5. C# 多线程学习总结

    C# 多线程学习总结 C#多线程学习(一) 多线程的相关概念 什么是进程? 当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程所组成的. ...

  6. Linux环境进程间通信(一):管道及命名管道

    linux下进程间通信的几种主要手段: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...

  7. Select * 一定不走索引是否正确?

    Select * 一定不走索引是否正确? 走索引指的是:SQL语句的执行计划用到了1.聚集索引查找  2.索引查找  ,并且查询语句中需要有where子句 根据where子句的过滤条件,去聚集索引或非 ...

  8. 用C#中实现的,调用CMD来执行BCP的代码

    用C#中实现的,调用CMD来执行BCP的代码 用c#中实现,调用cmd来执行bcp的代码,大家共享!引用空间:using System;using System.Data;using System.D ...

  9. LAMP on ubuntu12.04 PHP, Apache2, MySQL, Linux ( with phpmyadmin installed)

    there are several procedure which include: 1. Install the packages sudo apt-get install php5 php5-gd ...

  10. MacOSX64位机器上gcc编译32位x264静态库

    x264最新包地址:http://www.videolan.org/developers/x264.html 编译命令: ./configure --enable-static --host=i386 ...