The classes in the Reflection namespace give you the ability to view a class(objects) fields and methods. Overriding the ToString method provides a quick way to spew the objects current state to the console. You don't need to write code to access each field individually. This is particularly helpful if the class contains a lot of mebers.
The UsingGetValue method below provides some interesting insight on how FieldInfo::GetValue method works. The parameter to get value is the object instance for which to get the value. The call to get value in the code below is myClass1.UsingGetValue(myClass2);
// This will Print 10, World and not 5, Hello// Although we are calling this on myClass1 instance the method implementation calls GetVaue as// val = f.GetValue(target).ToString();//The target in the call below is myClass2//myClass1.UsingGetValue(myClass2); // As a result the members of target i.e. myClass2 will be returned and not myClass1.
#region
using
#endregion
namespace
{
}
myClass1.ToString();
// This will Print 10, World and not 5, Hello// Although we are calling this on myClass1 instance the method implementation calls GetVaue as// val = f.GetValue(target).ToString();//The target in the call below is myClass1.UsingGetValue(myClass2);
val = f.GetValue(
System.
val = mi.Name;
val = f.GetValue(target).ToString();