Directly or indirectly inherited from system. the attribute class is attribute. According to the Conventions, the attribute name should contain a suffix of attribute. Of course, this does not cause compilation errors, but the following content must be noted: Using System;
[Attributeusage (attributetargets. All)]
Public Class Xattribute: attribute
{}
[X] // Refers to xattribute
Class Class1 {}
[Xattribute] // Refers to xattribute
Class Class2 {}
[@ X] // Error: No attribute named "X"
Class Class3 {}
Read the above Code You should know that @ can remove the ambiguity. Another point is that [xattribute] does not have the ambiguity, even if there is another attribute called [xattributeattribute].
The other content is about attribute parameters, which can have two types of parameter location parameters (positional parameters) and named parameters (named parameters). See the following code: Using System;
[Attributeusage (attributetargets. Class)]
Public Class Helpattribute: attribute
{
Public Helpattribute ( String URL) {//Positional Parameter
}
Public String Topic { // Named Parameter
Get {}
Set {}
}
Public String URL {
Get {}
}
}
each common instance (not static) constructor can define a series of location parameters, while non-static read/write fields and attributes can define named parameters.
three reserved attributes:
1. system. attributeusageattribute is used to define how attributes are used. the class modified by this attribute must be directly or indirectly inherited from system. attribute, otherwise there will be a compilation error. there is an enumeration type location parameter attributetargets, which indicates what attributes can be applied. two named parameters allowmultiple (indicating whether the attribute can be used on a unified object multiple times) and inherited (indicating that when the attribute uses a base class, whether the property is automatically added to the derived class ).
2. system. diagnostics. conditionalattribute is used to mark a condition method. A condition is created by testing the compilation conditions. whether to call the method of the Flag attribute conditionalattribute depends on whether there are compilation conditions in the called place (not the called place. this attribute has a location parameter string conditionstring to indicate conditions. condition methods have the following restrictions:
A) condition properties can only be defined on classes or structured methods. If they are defined on interfaces, compilation errors may occur.
B) condition attributes can only be defined on methods with a return type of void.
C) condition attributes cannot be used in override-modified methods. the condition attribute can be applied to a virtual method and implicitly applied to methods that reload the method, but cannot explicitly define a method modified with override.
D) the condition attribute cannot be applied to methods that implement interfaces. otherwise, a compilation error occurs (the condition member ** method cannot implement the interface ** method ).
3. system. the obsolete is used to indicate that members of a type or type are no longer used. this attribute has three instance constructors, so there are three location parameter positioning sequences. A maximum of two parameters are string messages. (bool error indicates the message when the client code calls a member of this type or type. The default value is false. A warning is displayed when the code is commented out, if it is true, it is displayed as a compilation error.)