There are usage attributes in the code described earlier, all of which are defined by Microsoft, and as part of the. NET Framework class Library, many features are supported by the C # compiler.
. NET Frmework also allows you to define your own features. Custom attributes allow you to associate custom metadata with program elements. The metadata is created during compilation and embedded in the assembly. These features do not affect the compilation process because they are not recognized by the compiler, but they can be used as metadata in compiled assemblies when applied to program elements. These metadata are useful in document descriptions. What makes a custom attribute very important is the reflection (http://www.cnblogs.com/afei-24/p/6867986.html) technique, which the code can read and use to make decisions during the run.
I. Writing custom Attributes
[FieldName ("social")] Public string Socialnumber { ... }
When the C # compiler discovers that the Socialnumber attribute is applied with a fieldname attribute, it first appends the string attribute to the name fieldname, forming a combined name Fieldnameattribute, The fieldnameattribute class is then searched for all namespaces in its search path (that is, namespaces mentioned in the using statement). However, if the name of the attribute ends in a string attribute, the compiler does not add the string to the combined name.
So the above code is equivalent to:
[Fieldnameattribute ("social")] Public string Socialnumber { ... }
1.AttributeUsage properties
Custom attribute classes need to be derived directly or indirectly from System.Attribute. This class should also contain information about the control usage:
* attributes can be applied to which types of program elements (classes, structures, properties, methods, etc.)
* attribute can be applied to the same program element more than once
Br> * What are the required and optional parameters for this feature
define Fieldnameattribute attributes
[AttributeUsage (Attributetargets.property, Allowmultiple=false , Inherited=false )] public class Fieldnameattribute: Attribute { private string name; public fieldnameattribute (string name) { this . Name = name; } }
the attribute class Fieldnameattribute itself is marked with an attribute system.attributeusage. This is a feature that is defined by Microsoft and the C # compiler provides special support for it. AttributeUsage is primarily used to identify which types of program elements a custom attribute can be applied to. This information is given by its first parameter, AttributeTargets, which is required and whose type is enumerated type AttributeTargets. The above example specifies that the Fieldnameattribute attribute can only be applied to the attribute.
the members of the AttributeTargets enumeration are as follows:
All program elements that can apply the attribute are listed above. When applying an attribute to a program element, the attribute should be placed in the square brackets in front of the element:
[FieldName ("social")]
Public string Socialnumber
{
... ..
}
however, when applied to assembly and module, the attribute can be applied to the entire assembly or module, not to an element in the code, in which case this feature can be placed anywhere in the source code, However, you need to prefix the keyword assembly and module with:
[Assembly:fieldname ("social")]
[Module:fieldname ("social")]
when you specify a valid target element for a custom attribute, you can use the OR operator (|) to combine the values:
[AttributeUsage (Attributetargets.property |attributetargets.field,
Allowmultiple=false, Inherited=false)]
Public class Fieldnameattribute:attribute
{
private string name;
Public Fieldnameattribute (string name)
{
this.name = name;
}
}
You can also use AttributeTargets.All to specify that custom attributes can be applied to all types of program elements.
The AttributeUsage attribute also contains two additional parameters: AllowMultiple and inherited. They are specified in different syntax: parameter names = parameter values, not just the values of those parameters. These parameters are optional.
The allowmultiple parameter indicates whether an attribute can be applied more than once to the same item.
The inherited parameter indicates whether the attribute applied to the class or interface is automatically applied to the derived class or interface. If an attribute is applied to a method or property, it is automatically applied to their rewritten version.
2. Specifying attribute Parameters
[FieldName ("social")]
Public string Socialnumber
{
... ..
}
The compiler checks the arguments passed to the attribute (in this case, a string), merged the constructor with these parameters in the attribute class. If a matching constructor is found, the compiler passes the specified metadata to the assembly. If it is not found, a compilation error is generated. Reflection () reads the metadata from the assembly and instantiates the attribute classes they represent. Therefore, the compiler needs to ensure that such a constructor exists in order to instantiate the specified attribute during run time.
3. Optional parameters of the feature
In the AttributeUsage attribute, optional parameters are added to the attribute using parameter name = parameter value syntax. This syntax specifies the name and value of an optional parameter, which works through a public property or field in an attribute class:
[AttributeUsage (Attributetargets.property, allowmultiple=false, inherited=false)] Public classFieldnameattribute:attribute {Private stringname; PublicFieldnameattribute (stringname) { This. Name =name; } Public stringComment {Get;Set; }} [FieldName ("Social", comment="Optional parameter Values")] Public stringsocialnumber {...}
Custom Features for C #