C # custom features,

Source: Internet
Author: User

C # custom features,

The code described earlier has usage features. These features are defined by Microsoft. 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 features allow you to associate custom metadata with program elements. These metadata are created during compilation and embedded into the assembly. These features do not affect the compilation process because the compiler cannot recognize them, but they can be used as metadata in the compiled assembly when applied to program elements. These metadata are useful in the documentation. The reflection (http://www.cnblogs.com/afei-24/p/6867986.html) technology that makes user-defined properties play a major role in reading the metadata and using them for decision-making at runtime.


1. Write custom features

    [FieldName("Social")]        public string SocialNumber        {            ...        }

When the C # compiler finds that the SocialNumber Attribute applies a FieldName feature, it first adds the string Attribute to the name FieldName to form a combination name FieldNameAttribute, then, search for the FieldNameAttribute class in all the namespaces in the search path (that is, the namespaces mentioned in the using Statement. However, if the feature name ends with a string Attribute, the compiler will not add this string to the combination name.
Therefore, the above Code is equivalent:

    [FieldNameAttribute("Social")]        public string SocialNumber        {            ...        }

 

 

1. AttributeUsage features
Custom feature classes must be derived directly or indirectly from System. Attribute. This class should also contain information on control usage:
* Types of program elements (such as classes, structures, attributes, and methods) that can be applied by features)
* Can the feature be applied to the same program element multiple times?
* When a feature is applied to a class or interface, whether it is inherited by the derived class and Interface
* Required and optional parameters for this feature
If the compiler cannot find the corresponding feature class or finds such a feature class, but the method of using the feature does not match the information in the feature class, the compiler will generate a compilation error.

Define FieldNameAttribute features

    [AttributeUsage(AttributeTargets.Property, AllowMultiple=false, Inherited=false)]        public class FieldNameAttribute:Attribute        {            private string name;            public FieldNameAttribute(string name)            {                this.name = name;            }        }

The feature class FieldNameAttribute uses a feature System. AttributeUsage to mark. This is a feature defined by Microsoft. The C # compiler provides special support for it. AttributeUsage is used to identify the types of program elements that a custom feature can apply. This information is provided by AttributeTargets, the first parameter. this parameter is required and belongs to the enumeration type AttributeTargets. In the preceding example, the FieldNameAttribute attribute can only be applied to attributes.
The AttributeTargets enumerated members are as follows:
  
All program elements that can apply this feature are listed above. When applying features to program elements, put the features in square brackets before the elements:
[FieldName ("Social")]
Public string SocialNumber
{
...
}
However, when applied to Assembly and Module, features can be applied to the entire Assembly or Module, rather than to an element in the Code. In this case, this feature can be stored anywhere in the source code, but requires the use of the keyword Assembly and Module as the prefix:
[Assembly: FieldName ("Social")]
[Module: FieldName ("Social")]

When specifying valid target elements of a custom feature, you can use the OR operator (|) to combine these 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 the custom feature can be applied to All types of program elements.

The AttributeUsage feature also contains two other parameters: AllowMultiple and Inherited. They use different syntax to specify: parameter name = parameter value, rather than just the value of these parameters. These parameters are optional.
The AllowMultiple parameter indicates whether a feature can be applied to the same item multiple times.
The Inherited parameter indicates whether a feature applied to a class or interface is automatically applied to a derived class or interface. If a feature is applied to a method or attribute, it is automatically applied to their override version.

 2. Specify feature parameters
[FieldName ("Social")]
Public string SocialNumber
{
...
}
The compiler checks the parameters passed to the feature (in this example, it is a string) and generates the constructor that includes these parameters in the feature class. If a matched constructor is found, the compiler passes the specified metadata to the Assembly. If not found, a compilation error is generated. Reflection () reads metadata from the assembly and instantiates the feature classes they represent. Therefore, the compiler must ensure that such constructor exists to instantiate the specified feature during runtime.

  3. Optional features
In the AttributeUsage feature, use the parameter name = parameter value syntax to add optional parameters to the feature. This syntax specifies the name and value of an optional parameter, which takes effect through public attributes or fields in the feature class:

[AttributeUsage (AttributeTargets. property, AllowMultiple = false, Inherited = false)] public class FieldNameAttribute: Attribute {private string name; public FieldNameAttribute (string name) {this. name = name;} public string Comment {get; set;} [FieldName ("Social", Comment = "optional parameter value")] public string SocialNumber {...}

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.