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 directives using
System; using
System.Collections.Generic; using
System.Text; using
System.Reflection; #endregion
namespace
Main {
class MyClass {
public int someInteger = 10; //Some fields public string someMsg; // Some method. public MyClass(int i, string str) {
this.someInteger = i; this.someMsg = str; }
static void Main(string[] args) {
MyClass myClass1 = new MyClass(5, "Hello"); myClass1.ToString();
Console.WriteLine(); Console.WriteLine(); // The code below show how the GeValue method works. MyClass myClass2 = new MyClass(10, "World"); myClass2 with member values 10 and World. Console.WriteLine("Calling 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
myClass1.UsingGetValue(myClass2);
Console.WriteLine("Press any key ..."); Console.Read(); }
// Override to string to dump the objects field and method before calling the default base class implementation public override string ToString() {
string val; Type t = typeof(MyClass); // Get the type of the class we are interested..in this case ourself // Specifies flags that control binding and the way in which the search for members and types is conducted by reflection. BindingFlags bf = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Instance; Console.WriteLine("Filed Info:"); FieldInfo[] fields = t.GetFields(bf); // Get the fileds into an array. foreach (FieldInfo f in fields) {
Console.Write("\nField Name: " + f.Name + " Value: "); try {
// The parameter to get value is the object instance for which to get the value val = f.GetValue(
this).ToString(); System.
Console.Write(val); }
catch (FieldAccessException excp) {
Console.WriteLine("FieldAccessException : " + excp.Message); }
catch (TargetException excp) {
Console.WriteLine("TargetException : " + excp.Message); }
catch (ExecutionEngineException excp) {
Console.WriteLine("ExecutionEngineException : " + excp.Message); }
catch (MemberAccessException excp) {
Console.WriteLine("MemberAccessException : " + excp.Message); }
catch (Exception excp) {
Console.WriteLine("Exception : " + excp.Message); }
}
Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine("Method Info:"); MethodInfo[] methodInfo = t.GetMethods(bf); foreach (MethodInfo mi in methodInfo) {
Console.Write("\nField Name: " + mi.Name + " Value: "); try {
val = mi.Name;
System.
Console.Write(val); }
catch (MethodAccessException excp) {
Console.WriteLine("MethodAccessException : " + excp.Message); }
catch (TargetException excp) {
Console.WriteLine("TargetException : " + excp.Message); }
catch (ExecutionEngineException excp) {
Console.WriteLine("ExecutionEngineException : " + excp.Message); }
catch (MemberAccessException excp) {
Console.WriteLine("MemberAccessException : " + excp.Message); }
catch (Exception excp) {
Console.WriteLine("Exception : " + excp.Message); }
}
return base.ToString(); }
public void UsingGetValue(MyClass target) {
string val; Type t = typeof(MyClass); // Get the type of the class we are interested..in this case ourself // Specifies flags that control binding and the way in which the search for members and types is conducted by reflection. BindingFlags bf = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Instance; Console.WriteLine("Filed Info:"); FieldInfo[] fields = t.GetFields(bf); // Get the fileds into an array. foreach (FieldInfo f in fields) {
Console.Write("\nField Name: " + f.Name + " Value: "); try {
// The parameter to get value is the object instance for which to get the value val = f.GetValue(target).ToString();
System.
Console.Write(val); }
catch (FieldAccessException excp) {
Console.WriteLine("FieldAccessException : " + excp.Message); }
catch (TargetException excp) {
Console.WriteLine("TargetException : " + excp.Message); }
catch (ExecutionEngineException excp) {
Console.WriteLine("ExecutionEngineException : " + excp.Message); }
catch (MemberAccessException excp) {
Console.WriteLine("MemberAccessException : " + excp.Message); }
catch (Exception excp) {
Console.WriteLine("Exception : " + excp.Message); }
}
}
}
}