C # custom Attribute

Source: Internet
Author: User

Attribute Class is called a feature. It is a Modifier that can be freely defined by users and can be used to modify various objects to be modified. Class, interface, attribute, method, etc. unlike annotations, annotations are discarded by the compiler when the program is compiled. Therefore, Annotations do not affect program execution. attribute is a part of the program code. It will not only be discarded by the compiler, but also be compiled into the Metadata (Metadata) of the Program (Assembly). When the program is running, you can extract the additional information from the metadata at any time to determine how the program runs.


In the Framework, when we call a method, we will prompt that this method is out of date and replaced by a certain method. This is a very useful Attribute.

Custom Attribute

Make an Attribute that modifies the custom type

All custom attributes must be derived from Attribute. The AttributeUsage Attribute is used to specify the type of the element to be applied.

AttributeUsage has three parameters.

ValidOn

This Attribute can be customized and placed on those elements. The default value is all. The elements that can be operated include classes, sets, interfaces, fields, attributes, methods, and events.

[AttributeUsage (AttributeTargets) 4, AllowMultiple = false, Inherited = false)], 4 indicates the "class" element, and other elements such as 1 represent "assembly ", 16383 indicates "all", etc.) or ". "operator binds several AttributeTargets values. (Note: The default value is AttributeTargets. All)

AllowMultiple

This attribute identifies that our custom attribte can be used multiple times on the same language element. (Note: This attribute is of the bool type. The default value is false, meaning that the custom attribute can only be used once on the same language element)


Inherited

This attribute controls the inheritance rules of custom attribute classes. This attribute identifies whether our custom attribute can be inherited by a derived class. The default value is false.

Here is a small example.

Namespace Attribute1 {classProgram {staticvoid Main (string [] args) {// The typeof operator obtains a Type object associated with the AnyClass class. // The Student class information is obtained through reflection, you can also define the Type class // Typeinfo = typeof (Studnet) System. reflection. memberInfoinof = typeof (Student); Hobbyhobbyattr = (holobby) Attribute. getCustomAttribute (inof, typeof (holobby); if (hobbyattr! = Null) {// obtain the class description and basic usage Console. writeLine ("Class Name: {0}", inof. name); Console. writeLine ("interest type: {0}", hobbyattr. type); Console. writeLine ("interest index: {0}", hobbyattr. level) ;}} [holobby ("Sports", Level = 5)] classStudent {publicstring regression Sion; publicstring regression sion {get {return Regression Sion ;} set {customization sion = value ;}} [AttributeUsage (AttributeTargets. all, AllowMultiple = false, Inherited = true)] classholobby: Attribute {publicholobby (string _ type) {this. type = _ type;} // interest type privatestring Type; publicstring type {get {return type;} set {type = value ;}/// interest index privateint level; publicint Level {get {return level;} set {level = value ;}}}}

Attribute is widely used. This is just the beginning. For its deeper usage, it can be used as a validator to verify the underlying data.

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.