A notable difference between C # and C + + is that it provides support for metadata: data about other entities such as classes, objects, methods, and so on. Attributes can be grouped into two categories: one in the form of a part of the CLR, the other for the properties we create ourselves, and the CLR properties used to support serialization, alignment, and COM synergy. Some properties are for a group, some properties are for classes or interfaces, and they are also called attribute targets.
Attributes can be used for their property targets by placing them in square brackets before the property target.
We can create custom attributes at will, and use them when we think it's appropriate. Suppose we need to track bug fixes, we need to build a database that contains bugs, but we need to bind bug reports to special fixes, and you might add comments like the following in your code:
bug323fixedbyjesseliberty1/1/2005.
In this way, the bug can be clearly understood in the source code, but it might be better to keep the relevant data in the database, which is more convenient for our query work. If all bug reports use the same syntax, that's better, but then we need a custom attribute. We may use the following to replace the comments in the code:
AttributeUsage is an attribute-━━ meta attribute that provides metadata for metadata, or data about metadata. In this case, we need to pass two parameters, the first is the target (in this case, the class member). , and the second is a token that indicates whether a given element can accept more than one attribute. The value of AllowMultiple is set to true, meaning that a class member can have more than one Bugfixattribute property. If you want to combine two property targets, you can use the OR operator to connect them.
The above code will make an attribute subordinate to a class or an interface.
The new custom attribute is named Bugfixattribute. The named rule is to add attribute after the property name. After assigning attributes to an element, the compiler allows us to invoke this property using a thin property name. Therefore, the following code is legal:
The compiler first looks for the attribute named Bugfix, and if it is not found, finds Bugfixattribute.
Each property must have at least one constructor. Properties can accept two types of parameters: Environment parameters and named parameters. In the previous example, the BugID, the programmer's name and date are ambient parameters, and the annotation is a named parameter. Environment parameters are passed to the constructor and must be passed in the order defined in the constructor.
The data is stored with the metadata. Here is the complete source code and its output:
Custom properties
Usingsystem;
To create a custom attribute that is assigned to a class member
[AttributeUsage (AttributeTargets.Class,
Allowmultiple=true)]
PublicclassBugFixAttribute:System.Attribute
{
Custom property Builder for positional parameters
Publicbugfixattribute
(Intbugid,
Stringprogrammer,
StringDate)
{
This.bugid=bugid;
This.programmer=programmer;
This.date=date;
}
Publicintbugid
{
Get
{
Returnbugid;
}
}
Properties of named parameters
Publicstringcomment
{
Get
{
Returncomment;
}
Set
{
Comment=value;
}
}
Publicstringdate
{
Get
{
Returndate;
}
}
Publicstringprogrammer
{
Get
{
Returnprogrammer;
}
}
Proprietary member data
Privateintbugid;
Privatestringcomment;
Privatestringdate;
Privatestringprogrammer;
}
As we can see, attributes have absolutely no effect on the output, and creating properties does not affect the performance of the code. So far, the reader is only listening to me about the properties of the problem, using ILDASM to browse the metadata, you will find that the property does exist.
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