使用 Visual Studio 中的即时(或监视)窗口(我使用的是 VS2015 社区版),可以在中断模式下访问类的属性或方法。但是,对于从另一个类派生的类,如果基类的成员已在派生类中被覆盖,我将找不到访问基类成员的方法,即使从代码中可以直接执行此操作,如本示例所示:
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}";
}
如果我运行此代码,输出(显然)是:
我是鸵鸟,不是动物
Name
如果我在类的属性里面设置断点Ostrich
,然后Name
在立即窗口中检查属性,输出如下图:
?this.Name
"ostrich, not an animal"
相反,如果我要求运行基类的实现,我希望输出是“动物”。事实上,我明白了:
?base.Name
"ostrich, not an animal"
这似乎不仅没有帮助,而且实际上是误导/不正确的:我宁愿返回一个错误而不是错误的答案。
使用 Watch 窗口,只显示派生类的实现:
有什么方法可以使用立即窗口来访问类基类的被覆盖成员?