Get protected property value of base class using reflection

Assuming the following parent and child class:

class BaseClass
{
    private string privateField = "I'm Private";
}

class ChildClass : BaseClass
{

}

You can read privateField's value from a ChildClass instance using reflection like this:

ChildClass childInstance = new ChildClass();
object privateFieldValue = childInstance.GetType().BaseType
    .GetField("privateField", BindingFlags.NonPublic | BindingFlags.Instance)
    .GetValue(childInstance);
Console.WriteLine(privateFieldValue); // I'm Private

To access ConfigurationId property i have used following code:

SubsetController controller = new SubsetController(new CConfigRepository(new FakeDataContextRepository()));

var myBaseClassProtectedProperty=
            controller.GetType().BaseType
                .GetProperty("CCITenderInfo", BindingFlags.NonPublic | BindingFlags.Instance)
                .GetValue(controller);

var myProtectedProperty =
            CCITenderInfo.GetType()
                .GetProperty("ConfigurationId", BindingFlags.Public |     BindingFlags.Instance)
                .GetValue(myBaseClassProtectedProperty);

Tags:

C#

Dictionary