Reflecting a private field from a base class

You can manually go up in the inheritance chain to get the base fields:

Given these classes:

class SuperClass1
{
    private int myField;
}

class SuperClass2 : SuperClass1
{
}

class MyClass : SuperClass2
{

}

This should work:

var myObj = new MyClass();
var myField = typeof(MyClass).BaseType
                             .BaseType
                             .GetField("myField", BindingFlags.Instance | BindingFlags.NonPublic);

There’s a more generic solution in this SO answer: Not getting fields from GetType().GetFields with BindingFlag.Default

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)