Reflecting of attribute via il in CLR Exploration

Source: Internet
Author: User
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.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.