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

 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=
上一篇:R绘图 第十一篇:统计转换、位置调整、标度和向导(ggplot2)


下一篇:通过纯真IP地址实现根据用户地址显示信息