2011-05-03 510 views
37

当我尝试在设计时在即时窗口来评估表达,我得到错误:立即窗口中,“表达无法计算......”

The expression cannot be evaluated while in design mode.

如果我编译ASP.NET项目,并尝试以调试模式运行它我得到另一个错误:

The expression cannot be evaluated while in run mode.

为什么我会得到这些错误?过去我使用了立即窗口,即使在设计模式下也能正常工作。

+0

你想评估什么样的表情? – 2011-05-03 09:10:47

+1

我得到任何表达式,即使在2 + 2上 – Tomas 2011-05-03 09:14:57

回答

26

假设您不缺少立即窗口中的>操作符,如果您试图在多项目解决方案甚至Web项目中设计时评估表达式,可能会出现问题。

根据MSDN

If you are attempting to evaluate a function in a project that is not the startup project for the solution and you receive an error, try selecting the project in Solution Explorer and attempt the evaluation again.

另外:

You cannot use design time expression evaluation in project types that require starting up an execution environment, including Visual Studio Tools for Office projects, Web projects, Smart Device projects, and SQL projects.

+15

您也必须处于断点才能使用立即窗口或查看本地窗口中的对象。 – northben 2013-04-04 17:04:06

+1

这不是@northben!只需在解决方案资源管理器中选择一个库项目(如bflow1所示),然后在即时窗口中键入1 + 1。它会被执行! – 2013-10-21 15:44:35

+1

我认为问题在于即时窗口不会在我的应用程序运行时评估表达式,除非它在断点上。我现在主要在Linux上进行Python开发,所以我无法轻松打开VS来验证这一点。但无论哪种方式,如果你有它的工作,太棒了! – northben 2013-10-21 15:59:29

5

值得一提的是,立即窗口的行为取决于Visual Studio中的版本使用的是各不相同。如果我尝试在Visual Studio 2013 Express for Web中评估像? 2+2这样的简单表达式,我会收到“在设计模式下无法评估表达式”错误消息;但是,在Visual Studio 2013 Professional中,表达式的计算结果为4,而不必处于调试模式。

0

在我的情况下,在启用本机调试后使用Excel Interop时收到此错误。然后在调试模式下我想这:

?xlworkbook.sheets(1).name 

过程挂起,我没有收到任何回答,之后,所有其他的事情,例如?2+2给了我这个错误:

The expression cannot be evaluated while in run mode

为了能够再次使用调试功能,我不得不关闭本机调试

4

由于northben pointed out in a comment,如果你想访问的属性在即时窗口,而您的应用程序没有运行,你可能会得到:

The expression cannot be evaluated while in design mode.

因此:

  1. 坐落在一个断点你的应用程序将运行的文件;
  2. 等待应用程序执行停止(通过断点或异常)或手动触发(例如:转到URL);
  3. 在即时窗口中输入您想要访问的属性(例如GlobalConfiguration.Configuration) - 现在,如果该属性存在于该上下文中,则会得到适当的结果。如果它不存在,那么你将得到:

    The expression cannot be evaluated while in run mode.

这是为确保您访问在正确的上下文的属性一样简单。