简体   繁体   English

如何通过Visual Studio中的“即时”窗口访问派生类的基类的成员?

[英]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. 使用Visual Studio中的“即时”(或“监视”)窗口(我正在使用VS2015社区版),可以在中断模式下访问类的属性或方法。 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: 如果我在Ostrich类的Name属性内设置断点,然后在Instant窗口中检查Name属性,则输出如下所示:

?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. 我认为很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而不是Ostrich的属性,则可以将其括弧式转换为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. 问题在于,即使这仍然会给您带来ostrich而不是animal ,因为这恰恰是应该实现的行为,即您可以访问您认为属于Animal类型的对象的Name属性,但是其功能可以在派生类中重写。

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. 从编译器的角度来看,我不确定它的来龙去脉,但是如果基本实现的代码甚至没有出现在已编译的Ostrich ,也不会感到惊讶,除非Ostrich可以访问的代码base.

I agree that the fact that the Immediate window allows you to use base. 我同意即时窗口允许您使用base.的事实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. 然后给您一个错误的答案,这很容易引起混淆,并且很可能是Visual Studio中的错误,除非其他人可以解释这是怎么回事。

It would be interesting to see how the Immediate window behaves if you include some code in Ostrich which accesses base.Name 如果您在Ostrich包含一些访问base.Name代码, base.Name窗口的行为将很有趣。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在派生1和派生2中而不是基类中访问结果 - How can I access the result in Derived 1 and Derived 2 and not the base class 如何在派生类中调用,访问基类的公共成员 - How to invoke, access public members of base class in derived class 如何在调用基类的静态函数之前设置派生的静态成员 - How can I set derived static members before calling static functions of the base class 如何在visual studio的即时窗口中运行foreach循环? - How can I run a foreach loop in the immediate window of visual studio? 如果派生类没有参数化构造函数,如何从派生类中调用基类的参数化构造函数? - How can I call a base class's parameterized constructor from a derived class if the derived class has no parameterized constructor? 如何从接口访问派生类成员? - How to access derived class members from an interface? 如何在不使用方法或构造函数的情况下直接访问派生 class 中的基本 class 属性? - How can I directly access my base class property in the derived class without using methods or constructors? 如何在派生类中隐藏基类公共属性 - How can I hide a base class public property in the derived class 为什么我不能在没有强制转换的情况下访问基类的成员? - Why can I not access members of the base class without casting? 如何实例化基类,然后将其转换为派生类? - How can I instantiate a base class and then convert it to a derived class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM