Using the Immediate (or Watch) window in Visual Studio (I'm using VS2015 Community Edition), it's possible to access properties or methods on classes while in break mode. However, for a class derived from another class I can't find a way to access the base class's members if they have been overridden in the derived class, even though this is straightforward to do from code as shown in this example:
public class Program
{
static void Main(string[] args)
{
var ostrich = new Ostrich();
ostrich.WriteType();
Console.ReadKey();
}
}
public class Animal
{
public void WriteType()
{
Console.WriteLine("I'm an {0}", this.Name);
}
public virtual string Name => "animal";
}
public class Ostrich : Animal
{
public override string Name => $"ostrich, not an {base.Name}";
}
If I run this code, the output is (obviously):
I'm an ostrich, not an animal
If I set a breakpoint inside the Name
property of the Ostrich
class, then check the Name
property in the Immediate window, the output is as shown:
?this.Name
"ostrich, not an animal"
If instead I ask for the base class's implementation to be run, I'd expect the output to be "animal". In fact, I get this:
?base.Name
"ostrich, not an animal"
This seems to be not only unhelpful but actually misleading/incorrect: I'd rather an error were returned than the wrong answer.
Using a Watch window, only the derived class's implementation is shown:
Is there any way to use the Immediate window to access the overridden members of a class's base class?