A feature is a target element. It can contain additional information such as datasets, modules, classes, attributes, methods, and even function parameters. It is similar to annotations, but can be obtained through reflection at runtime. Customization features are mainly used in serialization, compiler instructions, and design patterns.
Multiple features can be applied to the same element and are separated by commas. The following expression rules are valid: [AttributeUsage] [Flags], [AttributeUsage, Flags], [Flags, attibuteUsageAttribute], [AttributeUsage (), FlagesAttribute ()]
The attivity instance is initialized during the compilation period, not during the runtime.
C # allows a specified prefix to represent the target element applied by the feature. We recommend that you handle this because explicit processing can eliminate the possible ambiguity. For example:
Namespace Anytao.net
{
[Assembly: MyAttribute (1)] // apply to assembly
[Moduel: MyAttribute (2)] // application module
Pubic class Attribute_how2do
{
//
}
}
AttributeUsageAttributeFunction: Specifies the usage of another attribute class.
The previous features are often applied when new features are defined.
[AttributeUsageAttribute (AttributeTarget. All ),
AllowMultiple = true,
Inherited = true]
Class MyNewAttribute: System. Attribute
{
//
}
- The feature does not affect any function of the application element, but only defines the characteristics of the element.
- All non-abstract features must have public access restrictions.
- Features are often used in Compiler commands, breaking through # define, # undefine, # if, # endif restrictions, and are more flexible.
- This feature is often used to obtain code comments at runtime and optimize debugging with additional information.
- Features can be applied in some design patterns, such as factory patterns.
- Features are also commonly used in BIT tag, unmanaged function tag, method discard tag, and other aspects.
The following is a detailed Attribute application Tutorial:
Application of Attribute in. net Programming (1)
Application of Attribute in. net Programming (2)
Application of Attribute in. NET Programming (3)
Application of Attribute in. NET Programming (4)
Application of Attribute in. NET Programming (5)
Application of Attribute in. NET Programming (6)