在网上看到这样一段定义
MADN的定义为:公共语言运行时允许添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型、字段、方法和属性等。Attributes和Microsoft .NET Framework文件的元数据(metadata)保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为。
我总结一下,包括以下几个核心的概念:
Attribute是类;Attribute功能上类似描述、注释;作用的对象是程序的元素,包括了class,method,property,struct等等;Attribute与.NET的元数据保存在一起,作为一种描述程序的元数据而存在。
Attribute是一种元数据描述,这种描述能够做到:
向.NET运行时,描述程序自身;影响程序的行为。因此,有着多种用途,例如序列化;安全;防止编译器优化,而方便调试;等等。
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.
转载于:https://www.cnblogs.com/warnet/p/4891641.html