1. Introduction
Attribute is a technical highlight introduced by the. NET Framework. Therefore, it is necessary for us to spend some time entering a portal to discover attribute. Because. NET Framework uses a large number of custom features to completeCodeConventions: [serializable], [flags], [dllimport], and [attributeusage]. I believe we have all seen them. Do you know the technology behind it.
When it comes to features, the history of Advanced Language Development reminds us of another well-known name: attribute. The impact of features and attributes on concepts that are often confusing beginners or those who transfer from C ++ to C. Then, what is an attribute and what is a feature, the concepts and differences between the two, usage and examples will be summarized and compared in this article, hoping to give you a better understanding. In addition, the topic of this article focuses on the introduction of features. The discussion of attributes focuses on the comparison between the two. More discussions on attributes will be discussed in detail in another topic.
2. introduction of concepts
2.1. What are features?
Msdn defines that a description declaration similar to a keyword can be added when the common language runtime is called attributes.ProgramThe elements in, such as the type, field, method, and attribute. The metadata of the attributes and Microsoft. NET Framework files is stored together and can be used to describe your code to the runtime, or affect the behavior of the application when the program is running.
In our summary, custom attribute is essentially a class that provides associated additional information for the target element and obtains additional information in reflection at runtime. The specific implementation methods of features will be further discussed in the following discussions.
2.2. What is an attribute?
Attribute is the basic concept of object-oriented programming. It provides access encapsulation for private fields. In C #, The get and set accessors are used to perform operations on readable and writable attributes, provides secure and flexible data access encapsulation. The concept of attributes is not the focus of this article, and we believe that most technical staff should have a clear concept of attributes. The following is a simple property example:
Public Class Myproperty { // Define Fields Private String _ Name; Private Int _ Age; // Define attributes and encapsulate the _ name field Public String Name { Get { Return (_ Name = Null )? String . Empty: _ name ;} Set {_ Name = Value ;}} // Define attributes and encapsulate the _ age Field // Add scope control Public Int Age { Get { Return _ Age ;} Set { If ( Value > 0 && Value & Lt; 150) {_ age = Value ;} Else { Throw New Exception (" Not a real age ");}}}} Public Class Mytest { Public Static Void Mian ( String [] ARGs) {myproperty = New Myproperty (); // Trigger the set accessors Myproperty. Name =" Anytao "; // Trigger get accessors Console. writeline (myproperty. Name); myproperty. Age = 66; console. writeline (myproperty. Age. tostring (); console. Readline ();}}
2.3. Differences and Comparison
Through clarification of concepts and historical backtracking, we know that features and attributes are only related to names. On msdn, attributes are interpreted in Chinese and even attributes, but I agree with the more common name: feature. In terms of functions and applications, the two are not too vague, so there is no need to compare the similarities and differences between their applications. This article focuses on the concept of features to discuss the application scenarios and rules.
The custom feature I understand is to add additional information to the target element, such as datasets, modules, classes, attributes, methods, and even function parameters, similar to annotations, however, it can be obtained through reflection at runtime. Customization features are mainly used in serialization, compiler instructions, and design patterns.
3. General rules
Custom features can be applied to target elements: Assembly, module, type, property, event, Field), method, Param, and return values should be complete.
Custom features are displayed in the form of [,] and placed on the adjacent elements. Multiple features can be applied to the same element. features are separated by commas. the following expressions 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. Example: using system;
Using System; Namespace Anytao.net {[Assembly: myattribute (1)] // Apply to the Assembly [Moduel: myattribute (2)] // Application module Pubic Class Attribute_how2 Do { // }}
Custom feature types must be inherited directly or indirectly from the system. Attribute Class, and the type must have a public constructor to create its instance.
All custom feature names should have an attribute suffix, which is a convention.
Custom features can also be applied to other custom features, which is well understood, because the custom features are also a class and comply with the public rules of the class. For example, many custom features apply attributeusageattribute to control how to apply the newly defined features.
[Attributeusageattribute (attributetarget. All ),
[Attributeusageattribute (attributetarget. All), allowmultiple = True , Inherited = True ] Class Mynewattribute: system. Attribute { // }
The custom 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.
Custom features are often used to obtain code comments during runtime and optimize debugging with additional information.
Custom features can be applied in some design patterns, such as the factory model.
Custom features are also commonly used in BIT tag, unmanaged function tag, method discard tag, and other aspects.
4. Application of features
4.1. Common Features
Common features, that is. net has provided inherent features, in fact in. the Net Framework has provided a wide range of inherent features for us to use. The following are some of the features that I think are the most commonly used and typical for simple discussion. Of course, this is just one of my words, also known. I want to know the features, or use it as the starting point. Starting from the classic version provided by. net, it may be a shortcut to knowledge, hoping to give you some inspiration.
Attributeusage
The attributeusage feature is used to control how custom features are applied to target elements. For more information about attributetargets, allowmultiple, inherited, and validon, see examples and other documents. We have already made a considerable introduction and examples, so let's learn more in practice.
Flags
The following uses the flags feature to regard enumerated values as bit tags rather than separate values. For example:
Enum Animal {dog = 0x0001, cat = 0x0002, duck = 0x0004, chicken = 0x0008}
Therefore, the following implementation is quite easy,
Animal animals = animal. Dog | animal. CAT;
Console. writeline (animals. tostring ());
Guess what the result is. The answer is: "dog, cat ". If no flags exists, the result here is "3 ". The bit mark will also be explained in the subsequent chapters of this series, so we will only discuss it here.
Dllimport
The dllimport feature allows us to call unmanaged code, so we can use the dllimport feature to introduce calls to Win32 API functions. For programmers who are used to unmanaged code, this feature is undoubtedly a treasure for saving lives.
Using System; Using System. runtime. interopservices; Namespace Anytao.net { Class Mainclass {[dllimport (" User32.dll ")] Public Static Extern Int MessageBox ( Int Hparent, String MSG, String Caption, Int Type ); Static Int Main (){ Return MessageBox (0 ," How to use attribute in. net "," Anytao_net ", 0 );}}}
Serializable
The serializable feature indicates that application elements can be serializated. serialization and deserialization are another topic that can be discussed in depth. Here we only propose the concept, in-depth research needs to be presented on a special topic. We will not go into detail here.
Conditional
The conditional feature is used for Conditional compilation and is used for debugging. Note: Conditional cannot be applied to data members and attributes.
Other important features include description, defaultvalue, category, readonly, and browerable, which can be studied in depth.
4.2. Custom features
Since attribute is essentially a class, we can customize more specific attributes to meet Personalized Requirements, as long as we comply with the above 12 rules, it is easy to implement a custom feature. The typical implementation method is:
Define features
[Attributeusage (attributetargets. Class | attributetargets. method, inherited = True )] Public Class Testattribute: system. Attribute { Public Testattribute ( String Message ){ Throw New Exception (" Error: "+ Message );} Public Void Runtest () {console. writeline (" Testattribute here. ") ;}} Application target element [test (" Error here. ")] [Test (" Error here. ")] Public Void Cannotrun (){ // }
Obtain additional element Information
If there is no mechanism to obtain additional information about the attribute at runtime, the attribute has no meaning. Therefore,. net uses the reflection mechanism to obtain attribute information at runtime. The implementation method is as follows:
Public Static Void Main ( String [] ARGs) {tester T = New Tester (); T. cannotrun (); Type TP = Typeof (Tester); testattribute myatt = (testattriatt) attribute. getcustomattriatt (memberinfo) TP, Typeof (Testattriatt); myatt. runtest ();}
5. Classic example
5.1 yuan a dish
Let's take a look at the comments.
Using System; Using System. reflection; // Use reflection technology to obtain feature information Namespace Anytao.net { // Custom features can also be applied to other custom features, // Apply attributeusage to control how to apply the newly defined features [Attributeusageattribute (attributetargets. All, // You can apply any element. Allowmultiple = True , // Allow multiple applications Inherited = False )] // Do not inherit from the derived class // The feature is also a class, // Must inherit from the system. Attribute Class, // The naming convention is: "Class Name" + attribute. Public Class Myselfattribute: system. Attribute { // Define Fields Private String _ Name; Private Int _ Age; Private String _ Memo; // Its constructor must be defined. If it is not specified, the compiler provides the default constructor without parameters. Public Myselfattribute (){} Public Myselfattribute ( String Name, Int Age) {_ name = Name; _ age = age ;} // Define attributes // Obviously, features and attributes are not the same thing. Public String Name { Get { Return _ Name = Null ? String . Empty: _ name ;}} Public Int Age { Get { Return _ Age ;}} Public String Memo { Get { Return _ Memo ;} Set {_ Memo = Value ;}} // Define the Method Public Void Showname () {console. writeline (" Hello, {0} ", _ Name = Null ? " World. ": _ Name );}} // Application custom features // You can use myself or myselfattri as the feature name. // You can assign values to the memo attribute. [Myself (" Emma ", 25, memo =" Emma is my good girl. ")] Public Class Mytest { Public Void Sayhello () {console. writeline (" Hello, my.net world. ");}} Public Class Myrun { Public Static Void Main ( String [] ARGs ){ // How to determine the feature information through reflection Type TP = Typeof (Mytest); memberinfo info = TP; myselfattribute myattribute = (myselfattribute) attribute. getcustomattribute (info, Typeof (Myselfattribute )); If (Myattribute! = Null ){ // Hey hey, check the comment content at runtime. Is it nice? Console. writeline (" Name: {0} ", Myattribute. Name); console. writeline (" Age: {0} ", Myattribute. Age); console. writeline (" Memo of {0} is {1} ", Myattribute. Name, myattribute. Memo); myattribute. showname ();} // Multi-point reflection Object OBJ = activator. createinstance ( Typeof (Mytest); methodinfo MI = TP. getmethod (" Sayhello "); Mi. Invoke (OBJ, Null ); Console. Readline ();}}}
Don't think about anything. Try it by yourself.
5.2 others' rocks
Msdn believes that attributes describe how to serialize data, specify features for force security, and limit the optimization of the real-time (JIT) compiler, so that code can be easily debugged. Attribute can also record file names or code authors, or control the visibility of controls and members during form development.
Dudu boss collection SeriesArticleThe Application of attribute in. NET programming has many inspirations for your application and is worth studying.
Comrade Alexander's series of articles "teach you how to write ORM (6)" also has a good interpretation.
Idior's article "Basic Principles of remoting and its extension mechanism" is also rewarding.
6. Conclusion
Attribute is a special technology introduced by. net, but there are not many discussions in the blog, so I will share my own experiences and hope to provide an in-depth guidance on this technical point. Deep-seated applications, such as serialization, program security, and design patterns, can all mine shining gold. This is the technological charm of. net. I hope everyone can speak freely to improve and supplement the author's incompleteness in this aspect and the lack of in-depth cognition. This will be the author's greatest encouragement and motivation.