1. Introduction
The attribute is. NET Framework introduces a technical bright spot, so it is necessary for us to take some time to enter an entrance to discover attribute entry. Because a large number of customization features are used in the. NET framework to complete code contracts, the constructs of [Serializable], [Flags], [DllImport], [AttributeUsage], I believe we've all met, So do you know the technology behind it.
The historical reason for the development of advanced languages is reminiscent of another familiar name: attributes. Attributes and attributes are often confusing conceptual shocks to beginners or people who move from C + + to C #. So, what is the attribute, what is the attribute, the two concepts and differences, usage and examples, will be done in this article to summarize and compare the summary, hoping to bring you the understanding of the harvest. In addition, the theme of this article is mainly based on the introduction of features, the emphasis on attributes is highlighted in the comparison between the two, more on the attributes will be discussed in another topic in detail, please pay attention to.
2. Introduction of Concepts
2.1. What is a feature?
MADN is defined as: The common language runtime allows you to add a descriptive declaration that resembles a keyword, called attributes, that annotate elements in your program, such as types, fields, methods, and properties. Attributes and the metadata of the Microsoft. NET framework files are stored together to describe your code to the runtime, or to affect the behavior of the application when the program is running.
We simply conclude that the custom attribute attribute is essentially a class that provides associated additional information for the target element and, at runtime, is reflected to obtain additional information. The specific feature implementation approach continues to deepen in the ensuing discussion.
2.2. What is a property?
Attributes are the basic concepts of object-oriented programming, providing access encapsulation of private fields, and using get and set accessor methods in C # to manipulate readable writable properties, providing secure and flexible data access encapsulation. The concept of attributes is not the focus of this article, and it is believed that most technicians should have a clear idea of attributes. Here is a simple example of a property:
2.3. Differences and comparisons
By clarifying the concepts and historical backtracking, we know that attributes and attributes are only in the name of the dispute, on MSDN on the Chinese interpretation of attribute is even a property, but I agree with the more common salutation: characteristics. In the functional and application, the two actually do not have too many fuzzy concept crossover, so there is no need to compare the similarities and differences of its application. This paper focuses on the concept of feature to discuss its application situations and rules.
The customization feature I understand is to add additional information to the target element, which can be data sets, modules, classes, properties, methods, and even function parameters, similar to annotations, but can be obtained at runtime in the form of reflection. Customization features are mainly used in serialization, compiler directives, design patterns and so on.
3. General rules
The target elements that can be applied by a custom attribute can be: assembly (assembly), modules (module), type (type), property, event (events), fields (field), methods (method), Parameters (param), return values ( return), should be full.
Custom features are presented in [,] form, placed on adjacent elements, multiple attributes can be applied to the same element, attributes are separated by commas, and the following expression rules are valid: [attributeusage][flags], [AttributeUsage, Flags], [flags, Attibuteusageattribute], [AttributeUsage (), Flagesattribute ()]
The Attibute instance is initialized at compile time, not run time.
C # allows the specified prefix to be used to represent the target element to which the attribute is applied, and it is recommended that this be done, because explicit processing eliminates the possible two semantics. For example: using System;
Using System;
Namespace Anytao.net
{
[Assembly:myattribute (1)]//Application to Assembly
[Moduel:myattribute (2)]//Apply to Module
Pubic class Attribute_how2do
{
//
}
}
Custom attribute types must be inherited directly or indirectly from the System.Attribute class, and the type must have a public constructor to create its instance.
All custom attribute names should have an attribute suffix, which is a customary convention.
Custom features can also be applied to other customization features, which is also well understood, since custom attributes themselves are a class that adheres to the public rules of a class. For example, many times our custom customization feature applies the AttributeUsageAttribute feature to control how the newly defined attributes are applied. [AttributeUsageAttribute (Attributetarget.all),
[AttributeUsageAttribute (Attributetarget.all),
AllowMultiple = True,
inherited = True]
Class MyNewAttribute:System.Attribute
{
//
}
Custom features do not affect any functionality of the application element, but only the attributes of that element.
All non-abstract attributes must have public access restrictions.
Features are often used in compiler directives to break through #define, #undefine, #if, #endif的限制, and are more flexible.
Custom features are often used to obtain code annotation information during runtime to optimize debugging with additional information.
Custom features can be applied in some design patterns, such as Factory mode.
Custom attributes are also used for bit tags, unmanaged function tags, method discard tags, and more.