I am an absolute novice at reflection in C#. I want to use reflection to access all of private fields in a class, including those which are inherited.
I have succeeded in accessing all private fields excluding those which are inherited, as well as all of the public and protected inherited fields. However, I have not been able to access the private, inherited fields. The following example illustrates:
class A
{
private string a;
public string c;
protected string d;
}
class B : A
{
private string b;
}
class test
{
public static void Main(string[] Args)
{
B b = new B();
Type t;
t = b.GetType();
FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Instance);
foreach(FieldInfo fi in fields){
Console.WriteLine(fi.Name);
}
Console.ReadLine();
}
}
This fails to find the field B.a.
Is it even possible to accomplish this? The obvious solution would be to convert the private, inherited fields to protected fields. This, however, is out of my control at the moment.