using System;
using System.Reflection;
public class MyClass
{
public string myFieldA;
public string myFieldB;
public MyClass()
{
myFieldA = "A public field";
myFieldB = "Another public field";
}
}
public class FieldInfo_GetValue
{
public static void Main()
{
MyClass myInstance = new MyClass();
Type myType = typeof(MyClass);
try
{
FieldInfo[] myFields = myType.GetFields(BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine(myType);
for(int i = 0; i < myFields.Length; i++)
{
Console.WriteLine(myFields[i].Name);
Console.WriteLine(myFields[i].GetValue(myInstance));
}
}
catch(Exception e)
{
Console.WriteLine("Exception : {0}", e.Message);
}
}
}