C# “由于当前方法的代码已优化,无法计算表达式”是什么意思。意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/131628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 14:55:34  来源:igfitidea点击:

What does "Cannot evaluate expression because the code of the current method is optimized." mean?

提问by Esteban Araya

I wrote some code with a lot of recursion, that takes quite a bit of time to complete. Whenever I "pause" the run to look at what's going on I get:

我写了一些带有大量递归的代码,这需要相当长的时间才能完成。每当我“暂停”运行以查看发生了什么时,我都会得到:

Cannot evaluate expression because the code of the current method is optimized.

无法计算表达式,因为当前方法的代码已优化。

I think I understand what that means. However, what puzzles me is that after I hit step, the code is not "optimized" anymore, and I can look at my variables. How does this happen? How can the code flip back and forth between optimized and non-optimzed code?

我想我明白这意味着什么。然而,令我困惑的是,在我点击 step 后,代码不再“优化”,我可以查看我的变量。这是怎么发生的?代码如何在优化和非优化代码之间来回切换?

采纳答案by Nescio

The Debugger uses FuncEval to allow you to "look at" variables. FuncEval requires threads to be stopped in managed code at a GarbageCollector safe point. Manually "pausing" the run in the IDE causes all threads to stop as soon as possible. Your highly recursive code will tend to stop at an unsafe point. Hence, the debugger is unable to evaluate expressions.

调试器使用 FuncEval 来允许您“查看”变量。FuncEval 要求在 GarbageCollector 安全点的托管代码中停止线程。在 IDE 中手动“暂停”运行会导致所有线程尽快停止。您的高度递归代码往往会停在不安全的点上。因此,调试器无法计算表达式。

Pressing F10 will move to the next Funceval Safe point and will enable function evaluation.

按 F10 将移动到下一个功能安全点并启用功能评估。

For further information review the rules of FuncEval.

有关更多信息,请查看FuncEval规则

回答by Lamar

You are probably trying to debug your app in release mode instead of debug mode, or you have optimizations turned on in your compile settings.

您可能正在尝试在发布模式而不是调试模式下调试您的应用程序,或者您在编译设置中打开了优化。

When the code is compiled with optimizations, certain variables are thrown away once they are no longer used in the function, which is why you are getting that message. In debug mode with optimizations disabled, you shouldn't get that error.

使用优化编译代码时,某些变量一旦在函数中不再使用,就会被丢弃,这就是您收到该消息的原因。在禁用优化的调试模式下,您不应收到该错误。

回答by Anthony K

I believe that what you are seeing is a result of the optimisations - sometimes a variable will be reused - particularly those that are created on the stack. For example, suppose you have a method that uses two (local) integers. The first integer is declared at the start of the method, and is used solely as a counter for a loop. Your second integer is used after the loop has been completed, and it stores the result of a calculation that is later written out to file. In this case, the optimiser MAY decide to reuse your first integer, saving the code needed for the second integer. When you try to look at the second integer early on, you get the message that you are asking about "Cannot evaluate expression". Though I cannot explain the exact circumstances, it is possible for the optimiser to transfer the value of the second integer into a separate stack item later on, resulting in you then being able to access the value from the debugger.

我相信您所看到的是优化的结果 - 有时会重用一个变量 - 特别是那些在堆栈上创建的变量。例如,假设您有一个使用两个(本地)整数的方法。第一个整数在方法开始时声明,仅用作循环的计数器。您的第二个整数在循环完成后使用,它存储稍后写入文件的计算结果。在这种情况下,优化器可能会决定重用您的第一个整数,从而节省第二个整数所需的代码。当您尝试尽早查看第二个整数时,您会收到有关“无法计算表达式”的消息。虽然我无法解释确切的情况,

回答by No one

While the Debug.Break() line is on top of the callstack you can't eval expressions. That's because that line is optimized. Press F10 to move to the next line - a valid line of code - and the watch will work.

虽然 Debug.Break() 行位于调用堆栈的顶部,但您无法评估表达式。那是因为那条线被优化了。按 F10 移动到下一行 - 有效的代码行 - 手表将工作。

回答by No one

Friend of a friend from Microsoft sent this: http://blogs.msdn.com/rmbyers/archive/2008/08/16/Func_2D00_eval-can-fail-while-stopped-in-a-non_2D00_optimized-managed-method-that-pushes-more-than-256-argument-bytes-.aspx

微软朋友的朋友发来这个:http: //blogs.msdn.com/rmbyers/archive/2008/08/16/Func_2D00_eval-can-fail-while-stopped-in-a-non_2D00_optimized-managed-method-that -pushes-more-than-256-argument-bytes-.aspx

The most likely problem is that your call stack is getting optimized because your method signature is too large.

最可能的问题是您的调用堆栈正在优化,因为您的方法签名太大。

回答by No one

Had the same problem but was able to resolve it by turning off exception trapping in the debugger. Click [Debug][Exceptions] and set the exceptions to "User-unhandled".

有同样的问题,但能够通过关闭调试器中的异常捕获来解决它。点击【调试】【异常】,将异常设置为“用户未处理”。

Normally I have this off but it comes in handy occasionally. I just need to remember to turn it off when I'm done.

通常我会关闭它,但它偶尔会派上用场。我只需要记住完成后将其关闭。

回答by No one

Look for a function call with many params and try decreasing the number until debugging returns.

查找具有许多参数的函数调用并尝试减少数字直到调试返回。

回答by Ralph177

This drove me crazy. I tried attaching with Managed and Native code - no go.

这让我发疯了。我尝试附加托管和本机代码 - 不行。

This worked for me and I was finally able to evaluate all expressions :

这对我有用,我终于能够评估所有表达式:

  • Go into Project / Properties
  • Select the Build tab and click Advanced...
  • Make sure Debug Info is set to "full" (not pdb-only)
  • Debug your project - voila!
  • 进入项目/属性
  • 选择构建选项卡,然后单击高级...
  • 确保调试信息设置为“完整”(不是仅限 pdb)
  • 调试您的项目 - 瞧!

回答by Vin

I had this issue when I was using VS 2010. My solution configuration has (Debug) selected. I resolved this by unchecking the Optimize Code property under project properties. Project (right Click)=> Properties => Build (tab) => uncheck Optimize code

我在使用 VS 2010 时遇到了这个问题。我的解决方案配置选择了 (Debug)。我通过取消选中项目属性下的优化代码属性解决了这个问题。项目(右键单击)=> 属性 => 构建(选项卡)=> 取消选中优化代码

回答by Vilhelm

In my case I had 2 projects in my solution and was running a project that was not the startup project. When I changed it to startup project the debugging started to work again.

就我而言,我的解决方案中有 2 个项目,并且正在运行一个不是启动项目的项目。当我将其更改为启动项目时,调试再次开始工作。

Hope it helps someone.

希望它可以帮助某人。