-- Reflection usage

Source: Internet
Author: User
Using system;
Using system. reflection;
Using system. Globalization;
 
Public class myclass
{
Private string mystring;
Public myclass ()
{
Mystring = "old value ";
}
String getfield
{
Get
{
Return mystring;
}
}
}
 
Public class fieldinfo_setvalue
{
Public static void main ()
{
Try
{
Myclass myobject = new myclass ();
Type mytype = type. GetType ("myclass ");
Fieldinfo myfieldinfo = mytype. getfield ("mystring", bindingflags. nonpublic | bindingflags. instance );
// Display the string before applying setvalue to the field.
Console. writeline ("the field value of mystring is {0}.", myfieldinfo. getvalue (myobject ));
// Display the setvalue signature used to set the value of a field.
Console. writeline ("applying setvalue (object, object ).");
// Change the field value using the setvalue method.
Myfieldinfo. setvalue (myobject, "New Value ");
// Display the string after applying setvalue to the field.
Console. writeline ("the field value of mystring is {0}.", myfieldinfo. getvalue (myobject ));
// Set the field value to its old value.
Myfieldinfo. setvalue (myobject, "old value ");
Myfieldinfo = mytype. getfield ("mystring", bindingflags. nonpublic | bindingflags. instance );
// Display the string before applying setvalue to the field.
Console. Write ("the field value of mystring is {0}.", myfieldinfo. getvalue (myobject ));
// Display the setvalue signature used to set the value of a field.
Console. writeline ("applying setvalue (object, object, bindingflags, binder, cultureinfo ).");
// Change the field value using the setvalue method.
Myfieldinfo. setvalue (myobject, "new value", bindingflags. Public, null, null );
// Display the string after applying setvalue to the field.
Console. writeline ("the field value of mystring is {0}.", myfieldinfo. getvalue (myobject ));
}
Catch (exception E)
{
// Any exception generated is displayed.
Console. writeline ("exception: {0}", E. Message );
}
}
}
 

====================================
Http://kb.cnblogs.com/page/42051/

Http://kb.cnblogs.com/page/42279/

 

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.