2

I'm trying to set the private "visible" field on a variable of type BaseClass.

  • ChildClass
    • BaseClass
      • "visible" field

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  
{
}
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
  • 1
    Can you post definitions for your classes, and the reflection code you're using? – Yuck Dec 19 '11 at 18:01
  • You seem to be conflating the terms "property" and "field" in your question. Are you sure you're looking for the right thing? – Chris Farmer Dec 19 '11 at 18:07
  • You are intermixing the term field and property in your question. They are different concepts and Reflection provides different methods for accessing their definitions (GetProperties() vs GetFields() for instance). My guess is that your visible field is actually defined as a property. – dkackman Dec 19 '11 at 18:09
  • If you have an instance of CalcScene, why not make a public Visible property and don't use Reflection? – jrummell Dec 19 '11 at 18:11
  • @jrummell - I'm using reflection to edit values in a 3rd party component I'm using. And so I obviously cannot change the source code! – Robin Rodricks Dec 19 '11 at 18:13
  • That sounds very fragile. Any changes in the component could break your code. – jrummell Dec 19 '11 at 18:23

2 Answers2

3

you can try like this

    class B
    {
        public int MyProperty { get; set; }
    }

    class C : B
    {
        public string MyProperty2 { get; set; }
    }

    static void Main(string[] args)
    {
        PropertyInfo[] info = new C().GetType().GetProperties();
        foreach (PropertyInfo pi in info)
        {
            Console.WriteLine(pi.Name);
        }
    }

produces

    MyProperty2
    MyProperty
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
Glory Raj
  • 17,397
  • 27
  • 100
  • 203
  • What if both properties are private? – Robin Rodricks Dec 19 '11 at 18:42
  • @Jenko would you pls take a look at this link http://stackoverflow.com/questions/2267277/get-private-properties-method-of-base-class-with-reflection – Glory Raj Dec 19 '11 at 18:52
  • Does it work? I've tried and i could not make it work. Do you have any more info? I'm getting the error "Remoting cannot find field "visible" on type BaseClas" ... do you know why? – Robin Rodricks Dec 19 '11 at 21:30
1

Here is some code that demonstrates the difference between getting a field vs a property:

  public static MemberInfo GetPropertyOrField(this Type type, string propertyOrField)
  {
      MemberInfo member = type.GetProperty(propertyOrField, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
      if (member == null)
          member = type.GetField(propertyOrField, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);

      Debug.Assert(member != null);
      return member;
  }
dkackman
  • 15,179
  • 13
  • 69
  • 123
  • Thanks, but the FieldInfo is a non-null value! I've already been able to access the field for the "visible" property! – Robin Rodricks Dec 19 '11 at 18:15
  • 1
    Oh I see. You are trying to set a private field value on a base class using reflection. Didn't catch that. Is your code fully trusted and/or have the correct permissions? http://msdn.microsoft.com/en-us/library/6z33zd7h.aspx – dkackman Dec 19 '11 at 18:41