In C #, attribute is an element modifier that exists as a source code for a program, because sometimes we need to add some descriptive information to our code. When we do not want to use annotations or internal code to describe the information that is compiled as attribute code, the compiler will generate them into the metadata.
At the same time, attribute is also an object.
1[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method | 2Attributetargets.returnvalue | Attributetargets.property, AllowMultiple =true, inherited =true)] 3 PublicclassCountryattribute:attribute4 { 5 6 PublicCountryattribute () {}7 8 PublicCountryattribute (stringname)9 { Ten This. Name =name; One } A - PublicintPlayercount {Get;Set; } - PublicstringName {Get;Set; } the } - -[Country (" China")] -[Country ("America")] + PublicclassSportsman - { + PublicstringName {Get;Set; } A at[Country (Playercount =5)] - Public VirtualvoidPlay () {} - } - - PublicclassHoopster:sportsman - { in PublicOverridevoidPlay () {} -}
It is very convenient to use attribute. He is a program that will be compiled, but can be used like a comment. Rather than using annotations, attribute can identify the results of functions, return values, and so on in the execution results, and implement some more complex identity functions. However, according to some data attribute itself is not a modifier, but a class that is instantiated by the class, which can be seen by anti-compilation.
The attribute property in C #