I will not talk much about attribute details. You can refer to the introduction in a series of Dudu and anytao to discuss the generation and operation mechanisms of attribute.
Here, I only want to discuss attributes in the CLR environment from several aspects.
First, let's look at a reflecting in attribute example:
Using system;
Using system. reflection;
Public class attributesreflectingonattributes
{
Public static void main ()
{
Type type = typeof (complex );
Foreach (codereviewattribute ATT in
Type. getcustomattributes (typeof (codereviewattrites), false ))
{
Console. writeline ("reviewer: {0}", ATT. reviewer );
Console. writeline ("Date: {0}", ATT. date );
Console. writeline ("comment: {0}", ATT. Comment );
}
}
}
[Attributeusage (attributetargets. Class, allowmultiple = true)]
Public class codereviewattribute: system. Attribute
{
Public codereviewattribute (string reviewer, string date)
{
This. reviewer = reviewer;
This. Date = date;
}
Public String comment
{
Get
{
Return (comment );
}
Set
{
Comment = value;
}
}
Public String date
{
Get
{
Return (date );
}
}
Public String reviewer
{
Get
{
Return (reviewer );
}
}
String reviewer;
String date;
String comment;
}
[Codereview ("AA", "01-12-2000", comment = "Joe 'Code")]
[Codereview ("BB", "01-01-2000", comment = "revisit this section")]
Class Complex
{
}
In this way, the output result in this example is to output the parameters in the attribute of the complex class codereview.
The getcustomattributes method in the type class of system. When it is overwritten in the derived class, an array of Custom Attributes represented by system. type is returned.