奇怪的行为:立即窗口启动应用程序而不是评估表达式 [英] Weird behaviour: Immediate Window starting app instead of evaluating expression

查看:31
本文介绍了奇怪的行为:立即窗口启动应用程序而不是评估表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道下面这段代码是否真正相关,但为了完全公开,这里是我试图从立即窗口调用的代码:

I don't know that the following piece of code is really relevant, but for the sake of full disclosure here is the code I was trying to call from the Immediate Window:

abstract class Test
{
    public int x;

    public Test()
    {
        x = 5; 
    }
}

class TestImp : Test
{
    public int GetX()
    {
        return this.x;
    }
}

这只是一个测试,看看默认的基本构造函数是否被自动调用,或者我是否因为不记得而必须专门调用它.

It was just a test to see whether the default base constructor gets called automatically or if I have to call it specifically because I couldn't remember.

好的,问题来了.我将其输入到立即窗口中以查看结果:

Okay, on to the problem. I typed this into the Immediate Window to see the result:

new Mercury_Reports.TestImp().GetX();

它只是开始了我的应用程序,而不是评估表达式.我关闭了应用程序并再次尝试了两次并得到了相同的结果.下一次,我在 Program.cs 文件中放置了一个断点.然后,它没有像前 3 次那样启动应用程序然后点击断点,而是决定实际上只是评估我的表达式.

And rather than evaluating the expression it just started my application. I closed the app and tried again two more times and got the same result. The next time, I went and put a breakpoint in my Program.cs file. Then instead of starting the app like it did the last three times and then hitting the breakpoint, it decided to actually just evaluate my expression.

我以前在 Visual Studio IDE 中看到过一些奇怪的东西,但我认为这是最奇怪的东西之一.任何人都知道那里发生了什么?:)

I've seen some weird things in the Visual Studio IDE before, but I think this is one of the weirdest. Anyone have a clue as to what was going on there? :)

推荐答案

在不调试的情况下在立即窗口中计算表达式时,会发生以下过​​程

When evaluating an expression in the immediate window when you're not debugging the following process takes place

  • 将您的项目/应用程序二进制文件加载到托管进程中
  • 以静默方式将调试器附加到托管进程
  • 针对该调试器会话评估表达式

大多数情况下,这样做的方式很难检测到应用程序实际上正在运行.但有时应用程序的副作用会显示出来并揭示幕后真正发生的事情.

Most of the time this is done in a way that makes it hard to detect that the application is actually being run. But occasionally a side effect of the application shows through and reveals what's really going on under the hood.

编辑

一般来说,它不应该显示 UI.我可以想到一些晦涩的极端情况,虽然这种情况会发生,但不是 Visual Studio 错误.基本上都归结为相同的场景

In general it shouldn't be displaying the UI though. I can think of some obscure corner cases where this would happen though and not be a Visual Studio bug. The basically all come down to the same scenario though

评估的字符串会导致意外的副作用发生在正常程序流程中不会发生的时间.

The evaluated string causes an unintended side effect to happen at a time where it wouldn't happen during normal program flow.

这实际上比您在即时窗口中预期的更常见,因为它本质上是无序执行您的代码.通常,在执行 Program.Main 之前,您永远不会进入 new Mercury_Reports,但在即时窗口中,这正是发生的情况.这可能会产生令人讨厌的效果,例如重新排序静态类型构造函数

This is actually more common than you'd expect in the immediate window because it's essentially executing your code out of order. Normally you'd never get to new Mercury_Reports before executing Program.Main but in the immediate window this is exactly what happens. This can have nasty effects like re-ordering static type constructors

这里有一些意想不到的后果,可以通过即时窗口表达式浮现

Here are some unintended consequences which can surface via an immediate window expression

  • 导致类型被加载,从而导致它们的静态初始化器运行
  • 更改静态初始化程序的运行顺序
  • 表达式的返回类型有一个调试器执行的ToString方法
  • 表达式的返回类型具有调试器执行的 DebuggerDisplay

过去我曾看到静态构造函数案例导致 UI 显示.本质上,一个静态类型构造函数正在评估 MainForm.Instance(一个惰性创建属性).在正常的程序流程中,它被从 Program.Main 调用并运行,从那时起就可以使用了.在即时窗口中,虽然 Program.Main 没有运行.但是正在执行的表达式无意中加载了该类型,因此显示了一个简单的属性 getter 的 UI.

In the past I have seen the static constructor case cause UI to show. Essentially a static type constructor was evaluating MainForm.Instance (a lazy creation property). During normal program flow it was called from Program.Main was run and from then on was simply available. In the immediate window though Program.Main didn't run. But the expression being executed inadventently loaded that type and hence displayed the UI for what was a trivial property getter.

虽然这是一个非常模糊的角落案例.我想说这里最可能的原因是 Visual Studio 中的错误.调试是一件令人讨厌的事情,尤其是在执行实时代码时,这可能就是一种症状.

This is a pretty obscure corner case though. I'd say the most likely cause here is a bug in Visual Studio. Debugging is nasty business, especially when executing live code, and this is probably a symptom of that.

这篇关于奇怪的行为:立即窗口启动应用程序而不是评估表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆