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/