I'm trying to set the private "visible" field on a variable of type BaseClass.
- ChildClass
- BaseClass
- "visible" field
- BaseClass
I've successfully accessed the variable of type ChildClass, and the FieldInfo for the "visible" field on the BaseClass.
But when I try to set/get the value of the field, I get the error System.Runtime.Remoting.RemotingException: Remoting cannot find field 'visible' on type 'BaseClass'.
So Is there a way to "down cast" a variable of type ChildClass to BaseClass in order for the reflection to work?
Edit: The exact code I'm using:
// get the varible
PropertyInfo pi = overwin.GetProperty("Subject", BindingFlags.Instance|BindingFlags.Public);
CalcScene scene = (CalcScene) pi.GetValue(inwin, null);
// <<< scene IS ACTUALLY A TYPE OF DisplayScene, WHICH INHERITS FROM CalcScene
// get the 'visible' field
Type calScene = typeof(CalcScene);
FieldInfo calVisible = calScene.GetField("visible",BindingFlags.Instance|BindingFlags.NonPublic);
// set the value
calVisible.SetValue(scene, true); // <<< CANNOT FIND FIELD AT THIS POINT
The exact class structure:
class CalcScene
{
private bool visible;
}
class DisplayScene : CalcScene
{
}