当变量在 .NET 中获得特定值时,我可以设置断点吗?

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

Can I set a breakpoint when variable is getting a specific value in .NET?

.netvisual-studiodebuggingbreakpointsconditional-breakpoint

提问by Delashmate

I am using Visual Studio 2010, and I know this feature is available in C++.

我使用的是 Visual Studio 2010,并且我知道此功能在 C++ 中可用。

I need to debug some code, that changes a variable to several values. I want to debug the code in a specific case, when the variable getting a specific value. I know I can add if(var == value), but is there any elegant way to do it?

我需要调试一些代码,将变量更改为多个值。我想在特定情况下调试代码,当变量获得特定值时。我知道我可以添加if(var == value),但是有什么优雅的方法可以做到吗?

Another question, can I set a breakpoint when a variable is changed in general?

另一个问题,我可以在一般情况下更改变量时设置断点吗?

回答by JaredPar

It is certainly possible to set a condition like a variable receiving a certain value. This is known as a breakpoint condition. To create one, do the following.

当然可以设置条件,例如变量接收某个值。这称为断点条件。要创建一个,请执行以下操作。

  • Set a break point at the point the variable changes
  • Right click on the break point and select "Condition"
  • Type in the conditional like "theNewValue == 42"
  • 在变量改变的点设置断点
  • 右键单击断点并选择“条件”
  • 输入条件,如“theNewValue == 42”

Now the breakpoint will only hit when your conditional evaluates to true.

现在断点只会在您的条件评估为真时触发。

The second item you asked for, breaking when a variable's value changes for any reason, is known as a data breakpoint. These are only available for C++ code. It's not an option in C#, VB.NETor any other managed language.

您要求的第二项,当变量的值因任何原因更改时中断,称为数据断点。这些仅适用于 C++ 代码。它不是 C#、VB.NET或任何其他托管语言中的选项。

回答by Richard Ev

So long as you are using a Visual Studio edition other than Express, you can achieve this in C# using a breakpoint condition.

只要您使用的是 Express 以外的 Visual Studio 版本,您就可以在 C# 中使用断点条件来实现这一点。

In the Breakpoint Condition dialog box, enter a valid expression in the Condition box, such as myLocalVariable > 1

在 Breakpoint Condition 对话框中,在 Condition 框中输入有效的表达式,例如myLocalVariable > 1

and

...choose Has changedif you want to break when the value of the expression has changed.

...choose Has changed如果你想在表达式的值改变时中断。

To get to the Has changedoption, right-click your breakpoint in the Breakpointswindow and select Condition..., then check the screenshot below.

要进入Has changed选项,请在Breakpoints窗口中右键单击您的断点并选择Condition...,然后查看下面的屏幕截图。

Has Changed option for breakpoint conditions

已更改断点条件的选项

回答by BrokenGlass

Add a breakpoint with F9 - right click it and select "Condition..."- now you can add a boolean condition and the breakpoint will only get hit if that condition evaluates to true.

使用 F9 添加断点 - 右键单击​​它并选择"Condition..."- 现在您可以添加一个布尔条件,只有在该条件评估为真时才会命中断点。

回答by Pedro Maia Costa

It depends on the scope of your breakpoint. If the variable is not local or not static you won't be able to.

这取决于断点的范围。如果变量不是局部的或不是静态的,你将无法做到。

To set the condition of a breakpoint, right click it and you should get this screen:

要设置断点的条件,请右键单击它,您应该会看到以下屏幕:

Enter image description here

在此处输入图片说明

Pick Condition...

选择条件...

回答by thewhiteambit

You can use conditional breakpoints. I know your question was specific to VS2010, but be aware that from VS2012 on, you have to switch to the Managed Compatibility Mode, to use conditional breakpoints in Visual Basic. Why and how is described here:

您可以使用条件断点。我知道您的问题特定于 VS2010,但请注意,从 VS2012 开始,您必须切换到托管兼容模式,才能在 Visual Basic 中使用条件断点。为什么以及如何在这里描述:

switching-to-managed-compatibility-mode-in-visual-studio-2013

在visual-studio-2013中切换到托管兼容性模式

回答by cssyphus

VSCode

VS代码

In VisualStudio Code, you can set conditional breakpoints as follows:

在 VisualStudio Code 中,您可以按如下方式设置条件断点:

  1. Click in gutter to create a red-dot breakpoint

  2. Choose Debug from left-side toolbar (icon: circle-slash over bug)

  3. There are four sections: Variables, Watch, Call Stack and Breakpoints

  4. Expand Breakpoints section so you can see the breakpoints

  5. Right-Click on the desired breakpoint

  6. Choose Edit Breakpoint...

  7. Set your condition and press <Enter>. For example:
    myvar == 1234
    or
    'stophere' in myvar
    etc

  1. 单击装订线以创建红点断点

  2. 从左侧工具栏中选择调试(图标:错误上的圆圈斜线)

  3. 有四个部分:变量、观察、调用堆栈和断点

  4. 展开 Breakpoints 部分,以便您可以看到断点

  5. 右键单击所需的断点

  6. 选择 Edit Breakpoint...

  7. 设置条件并按 <Enter>。 例如:
    myvar == 1234

    'stophere' in myvar

References:

参考:

https://code.visualstudio.com/docs/editor/debugging#_conditional-breakpoints

https://code.visualstudio.com/docs/editor/debugging#_conditional-breakpoints

回答by i_am_jorf

You can do both of these things.

你可以做这两件事。

  1. Set the breakpoint in VS. Right click on the red dot in the margin and select Add Condition. In there you can say var==valueand select "Is True".
  2. You can probably achieve this with the "Has Changed" option in the dialog above.
  1. 在VS中设置断点。右键单击边距中的红点并选择添加条件。在那里你可以说var==value并选择“是真的”。
  2. 您可以使用上面对话框中的“已更改”选项来实现这一点。