Attribute Value at runtime?

Source: Internet
Author: User
Using system;

Using system. reflection;


Public class test

{

Public static void main ()

{

Testattribute TA = new testattribute ();

Type type = TA. GetType ();

Helpattribute help;

Foreach (attribute ATTR in type. getcustomattributes (true ))

{

Help = ATTR as helpattribute;

If (null! = Help)

{

Console. writeline (HELP. Description );

Help. Description = "the value has changed .";

Console. writeline (HELP. Description );

Console. Readline ();

}

}

Foreach (attribute ATTR in type. getcustomattributes (true ))

{

Help = ATTR as helpattribute;

If (null! = Help)

{

Console. writeline (HELP. Description );

Console. Readline ();

}

}

}

}

[Help ("the origin value")]

Public class testattribute

{

}

[Attributeusage (attributetargets. Class | attributetargets. method, allowmultiple = true, inherited = true)]

Public class helpattribute: attribute

{

Public helpattribute (string descrition_in)

{

This. Description = descrition_in;

}

Protected string description;

Public String description

{

Get

{

Return this. description;



}

Set

{

This. Description = value;

}



}

}


 

After running the above code, you can see that the answer is no,

The running result of this Code is:

The origin Value

The value has changed

The origin Value

We can see that attribute can change its value like a general class, but it is not the entity of attach in the testattribute class. I Want To (reflection and accessories I just started to study) reflect takes the attivity class entity of the attach class in the testattribute class out the read value, and creates another attribute entity when writing the attribute value. The above is just my test thought. It is absolutely wrong, correct your skills. Ah, you must learn Il, read the decompiled code, and see how. Net Works.

A netizen posted a post on csdn a few days ago and asked if a widget could change a property and affect the display of other attributes, [browsable (false)] is used to control the display. I answered the question based on my experience. This is a description. Later I thought that attribute is also a class, attributes may also be writable, so we did the above test.

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.