Attribute的理解和认识
1什么是Attribute?
在网上看到这样一段定义
MADN的定义为:公共语言运行时允许添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型、字段、方法和属性等。Attributes和Microsoft .NET Framework文件的元数据(metadata)保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为。
我总结一下,包括以下几个核心的概念:
- Attribute是类;
- Attribute功能上类似描述、注释;
- 作用的对象是程序的元素,包括了class,method,property,struct等等;
- Attribute与.NET的元数据保存在一起,作为一种描述程序的元数据而存在。
2为什么需要Attribute?
Attribute是一种元数据描述,这种描述能够做到:
- 向.NET运行时,描述程序自身;
- 影响程序的行为。
因此,有着多种用途,例如序列化;安全;防止编译器优化,而方便调试;等等。
3如何使用Attribute?
- 所有自定义的Attribute一定要继承自System.Attribute;
- 创建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参考链接
- http://www.cnblogs.com/luckdv/articles/1682488.html
- http://www.cnblogs.com/hyddd/archive/2009/07/20/1526777.html
- https://msdn.microsoft.com/zh-cn/library/system.attribute(v=vs.110).aspx?query=
Attribute的理解和认识的更多相关文章
- DOM中 property 和 attribute 详解
被问到 property 和 attribute 的区别,想来也是要好好看一下. 一.基本概念区别 其实Attribute和Property这两个单词,翻译出来都是“属性”,<js高级程序设计& ...
- 属性attribute和property的区别
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...
- Python2.2-原理之类型和运算
此节来自于<Python学习手册第四版>第二部分 一.Python对象类型(第4章) 1. Python可以分解成模块.语句.表达式以及对象:1.程序由模块构成:2.模块包含语句:3.语句 ...
- 着色器(Shader)
着色器(Shader) 顶点着色器(Vertex shader) 片段着色器(Fragment shader) 几何着色器(Geometry Shader) 提供通用计算能力的着色器(Compute ...
- 阶段02JavaWeb基础day02&03JavaScript
javascript知识体系 ECMAScript javascript与html结合方式 内部: <script type="text/javaScript">*** ...
- jQuery .attr() vs. .prop()
Property vs. Attribute 在开始正式比较prop()和attr()两个jQuery方法之前,我们有必要先弄清一下Property和Attribute两个单词的意思.在中文里面,它们 ...
- iOS - keychain 详解及变化
keychain介绍 iOS keychain 是一个相对独立的空间,保存到keychain钥匙串中的信息不会因为卸载/重装app而丢失, .相对于NSUserDefaults.plist文件保存等一 ...
- LDAP学习小结【仅原理和基础篇】
此篇文章花费了好几个晚上,大部分是软件翻译的英文文档,加上自己的理解所写,希望学习者能尊重每个人的努力. 我有句话想送给每个看我文章的人: 慢就是快,快就是慢!!! 另外更希望更多人能从认真从原理学习 ...
- django优化--ORM优缺点
谈Django绕不开ORM ORM : ORM概念,ORM特点,ORM 的优点,ORM 的缺点 orm : 对象关系映射 (Object Relational Mapping) ,用于实现面向对象编程 ...
随机推荐
- Asp.Net Web API 导航3
Asp.Net Web API 导航 Asp.Net Web API第一课:入门http://www.cnblogs.com/aehyok/p/3432158.html Asp.Net Web A ...
- 如何本地测试例如QQ登录等第三方接口
前言:现在基本是个网站就会集成第三方的一些接口,比如QQ登录.分享等等.但是在开发的时候,尤其是没有这方面经验的开发人员来说,调试流程时会显得迷茫,不知道怎么调试.这里就个人的这方面学习摸索做一个总结 ...
- TOGAF企业连续体和工具之架构资源库及架构工具的选择
TOGAF企业连续体和工具之架构资源库及架构工具的选择 3. 架构资源库 在一个企业,尤其是在一个大型企业中,建设一个成熟的架构往往会产生大量的工作产品.为了很好地管理和利用这些工作产品,企业需要制定 ...
- java 读取图片色深
问题: 想写一个小程序可读取图片的色深(bit-depth).网上有一些软件可完成这个功能,但是我想把程序做成一个可移植的插件. 本想用c写的,但实在麻烦,最后选择java,与很多方法不用自己写,速度 ...
- SQLSERVER之高灵活的业务单据流水号生成
SQLSERVER之高灵活的业务单据流水号生成 最近的工作中要用到流水号,而且业务单据流水号生成的规则分好几种,并非以前那种千篇一律的前缀+日期+流水号的简单形式,经过对业务的分析,以及参考网上程序员 ...
- 一步步学习Python-django开发-建立django数据库
上篇文章建立了一个空的网站.现在我们要准备正式我们的网站开发咯.那么开发的第一步是啥呢?当然是要确定做什么网站了.每个人的开发习惯可能不尽相同,有点可能是从前端开始设计,有的可能是从数据库开始设计(不 ...
- Android ListView中添加不同的多种布局
最近做项目要使用ListView加载不同的布局,由于自己写的代码不能贴出,故找了一篇自认为比较好的blog给分享出来,希望对用到此项技术的同学有点帮助. http://logc.at/2011/10/ ...
- mssql分页原理及效率分析
下面是常用的分页,及其分页效率分析. 1.分页方案一:(利用Not In和SELECT TOP分页) 语句形式: SELECT TOP 10 * FROM TestTable WHERE (ID NO ...
- [置顶] Nosql笔记(一)——关系型数据库回顾
Nosql笔记(一)——关系型数据库回顾 在平常的商业应用中,我们所使用的大多都是关系型数据库,诸如SQL Server. MY SQL. Oracle等. 关于关系型数据库中的关键技术: 存储引擎 ...
- android JNI--- 搭建环境(1)
搭建好我们的ndk开发环境,这部分网上资料一大堆,拿来主义~~~~ 工欲善其事必先利其器 , 下面介绍下 Eclipse SDK NDK Cygwin CDT 集成开发环境的搭建. 1.Android ...