简体   繁体   中英

How can I access members of a derived class's base class through the Immediate window in Visual Studio?

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?

I think base. is not publicly available outside the class. If you were writing code outside of the class implementation, to access the property of Animal rather than of Ostrich then you would bracket-cast to Animal .

((Animal)obj).Name

The problem is that even this will still give you ostrich not animal , since this is exactly the behaviour that overriding is supposed to achieve, ie that you can access the Name property of an object that you think is of type Animal , but that its functionality can be overridden in a derived class.

I'm not sure of the ins and outs of it from a compiler perspective, but it wouldn't surprise me if the code for the base implementation doesn't even end up in the compiled Ostrich , unless there is code in Ostrich which accesses base.

I agree that the fact that the Immediate window allows you to use base. and then gives you the wrong answer is confusing and likely a bug in Visual Studio, unless others can explain how this makes sense.

It would be interesting to see how the Immediate window behaves if you include some code in Ostrich which accesses base.Name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM