C# 调试时当前上下文中不存在变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13517521/
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-10 08:52:17  来源:igfitidea点击:

Variable does not exist in the current context while debugging

c#.netdebuggingpropertiesvisual-studio-2012

提问by Dominik Antal

I inserted two temp variables and want to see their values, but I can't. I could solve it by placing it somewhere else, but I'm interested why this behaviour exists.

我插入了两个临时变量并想查看它们的值,但我不能。我可以通过将它放在其他地方来解决它,但我对这种行为存在的原因很感兴趣。

   public float Value
    {
        get
        {
            float result = Memory.ReadFloat(Address);

            double Radian = Math.Round(result, 2); // **BREAK POINT HERE**
            double Degree = Math.Round(Math.Round((double)(result * 180 / Math.PI)), 2); // **BREAK POINT HERE**

            return result; // **BREAK POINT HERE**
        }
    }

I break on all three points, but I can't get Visual Studio 2012 to show me the values. Only result will show up on the locals window, there is no variable called Radian or Degree.

我打破了所有三点,但我无法让 Visual Studio 2012 向我显示这些值。只有结果会显示在局部窗口上,没有称为弧度或度数的变量。

If I add a watch for Radian variable for example, I get this message with a red cross icon:

例如,如果我为 Radian 变量添加监视,则会收到带有红色十字图标的消息:

Radian - The name 'Radian' does not exist in the current context

Radian - 当前上下文中不存在名称“Radian”

采纳答案by Lee

It's possible the local variables have been optimised away by the JIT compiler. Since you're using Visual Studio you might be able to switch the configuration to Debug and rebuild.

局部变量可能已经被 JIT 编译器优化掉了。由于您使用的是 Visual Studio,因此您可以将配置切换到调试和重建。

If not, you can configure the JIT compiler to disable optimisations and generate tracking information - see here on how to set the configuration. This should allow you to see local variable when you attach the debugger to the process.

如果没有,您可以配置 JIT 编译器以禁用优化并生成跟踪信息 -有关如何设置配置的信息,请参见此处。这应该允许您在将调试器附加到进程时查看局部变量。

回答by ishayle

I've encountered another scenario in VS2012 that causes variables to "disappear" while in debug mode:

我在 VS2012 中遇到了另一个导致变量在调试模式下“消失”的情况:

make sure you don't have this:

确保你没有这个:

if(false)
   {
   .
   }
else
   {
   //Code here will be optimized and variables will not be available.
   }

回答by Chewdoggie

If you are trying to debug in a release build (release mode instead of debug mode), you'll get this error. Change your solution configuration to Debug (Any CPU) and you'll be able to see variable values in the immediate window.

如果您尝试在发布版本(发布模式而不是调试模式)中进行调试,则会收到此错误。将您的解决方案配置更改为 Debug (Any CPU),您将能够在即时窗口中看到变量值。

回答by Crazy Cat

I changed my Solution Configuration to Debug (Any CPU) but that wasn't the problem. After upgrading to Visual Studio 2017 I thought I was in Debug but there was one more simple (but important) step. (And it's probably obvious to most, but I missed it.) Where “Solutions Configurations” is set to “Debug” I had to ALSO click the down arrow next to “Debug” and select “Configuration Manager…” and then in that corresponding popup window “Configuration” was still set to “Release” – I had to change that to “Debug” and click the “Close” button. Rerunning from there allowed me to view all variables while debugging.

我将我的解决方案配置更改为调试(任何 CPU),但这不是问题。升级到 Visual Studio 2017 后,我以为我处于调试状态,但还有一个更简单(但很重要)的步骤。(这对大多数人来说可能很明显,但我错过了。)在“解决方案配置”设置为“调试”的情况下,我还必须单击“调试”旁边的向下箭头并选择“配置管理器...”,然后在相应的弹出窗口“配置”仍然设置为“发布”——我必须将其更改为“调试”并单击“关闭”按钮。从那里重新运行允许我在调试时查看所有变量。

回答by NightShovel

I had a .NET Standard project, and what worked for me was to go:

我有一个 .NET Standard 项目,对我有用的是:

  • Project properties.
  • Build tab.
  • Advanced button (all the way at the bottom-right).
  • Change the "Debugging information" option to full.
  • List item
  • 项目属性。
  • 构建选项卡。
  • 高级按钮(一直在右下角)。
  • 将“调试信息”选项更改为完整。
  • 项目清单

It had been set to "Portable". Changing it to "Full" allowed me to see variables in Watch and Immediate windows again.

它已设置为“便携式”。将其更改为“完整”允许我再次在 Watch 和 Immediate 窗口中看到变量。

Not sure if it it's relevant, but I had recently converted the project from PCL to .NET Standard. Maybe something got lost in translation.

不确定它是否相关,但我最近将项目从 PCL 转换为 .NET Standard。也许在翻译中丢失了一些东西。