1. What is attribute?
Let's take a look at the three paragraphs below.Code:
1. custom attribute class: versionattribute
- [Attributeusage (attributetargets. Class)]
- Public class versionattribute: attribute
- {
- Public string name {Get; set ;}
- Public String Date {Get; set ;}
- Public String describtion {Get; set ;}
- }
2. Use the custom attribute class:
- [Version (name = "hyddd", date = "2009-07-20", describtion = "hyddd's class")]
- Public class mycode
- {
- //...
- }
3. How is the attribute in the above class generally used?
- Class Program
- {
- Static void main (string [] ARGs)
- {
- VaR info = typeof (mycode );
- VaR classattribute = (versionattribute) attribute. getcustomattribute (Info, typeof (versionattribute ));
- Console. writeline (classattrieline. Name );
- Console. writeline (classattrieline. date );
- Console. writeline (classattrition. describtion );
- }
- }
The example is complete! The above three pieces of code I believe have explained what attribute is and how to use it.
2. In-depth discussion of attribute
1. Definition of attribute
For the definition of the attribute concept, I directly reference the section "features and attributes of. Net that you must know" to describe:
Madn is defined as: A description declaration similar to a keyword can be added during the runtime of the public language. It 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.
Oh, the original attribute aims to provide additional information about the association for the element. In the above Code, "[attributeusage (attributetargets. Class)]" indicates that the element of attribute to provide additional information is class, so if the code in the second paragraph above is changed:
- Public class mycode
- {
- [Version (name = "hyddd", date = "2009-07-20", describtion = "hyddd's class")]
- Public void test (){}
- }
-
A compilation error occurs.
2. attribute as the Compilation instruction
Attribute classes are instantiated during compilation, rather than being instantiated at runtime as a normal class. Therefore, in the third code segment, you can obtain the attribute information of mycode without instantiating the mycode object. Because attribute classes are instantiated during compilation, you can use external tools to maintain these attributes.
3. Attribute and property
In Chinese, attribute and property are both called "attributes", which is easy to confuse. The currentArticleAttribute is generally translated as "feature", while property is called "attribute ".
Maybe you will ask, like static property/field, you can get some information without instantiation. If so, what is the significance of attribute?
1. Property:
Property can be said to be an object-oriented concept, providing 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. For example:
- Public class Robot
- {
- Private string name = ""; // field: Field
- Public string name // property: property, which encapsulates the field.
- {
- Get {return name ;}
- Set {name = value ;}
- }
- }
2. attribute:
Attribute aims to provide additional information for the element. It works more like annotations.
It can be said that property/field and attribute are two completely different concepts. Although they sometimes do the same thing, remember that they are essentially two different things.
Iii. Notes for implementing your own attribute
1. Custom Attributes must inherit system. attribute directly or indirectly.
2. Here is a convention: all custom feature names should have an attribute suffix. Because when your attribute is applied to an element of a program, the compiler first looks for your attribute definition. If not, then it looks for the definition of "attribute name" + attribute. If none of them are found, the compiler reports an error. This is why I can define a versionattribute In the first code above, but in the second code, I use the attribute version. :>
The following are some materials that may be used to develop custom attributes:
[1] attributes can be associated with the following elements:
Assembly, module, type, property, event, field, method, and Param), return value (return ). For example:
- [Assembly: Version (name = "hyddd", date = "2009-07-20", describtion = "hyddd's class")]
- Public class mycode
- {
- //......
- }
The specified prefix is used to represent the target element applied by the feature. We recommend that you handle this because explicit processing can eliminate the possible ambiguity.
[2] attributetargets goals include:
[3] Description of three properties in attributeusageattribute:
Iv. References
[1] msdn
[2] "features and attributes of. Net that you must know" is highly recommended by bloggers!
[3] attribute application in. NET Programming
[Edit recommendations]
- C # development of custom controls: pin and connector
- Compare the properties and/attribute of the C # custom control)
- C # component development: interoperability between COM and. Net objects
- Introduction to. NET platform, C #, and ASP. NET
- C # attributes: defines the design period information
[Responsible editor: Wang yuantel :( 010) 68476606]