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

发布于 2024-12-05 19:47:03 字数 186 浏览 1 评论 0原文

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

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

还有一个问题,一般情况下变量改变的时候可以设置断点吗?

I am using Visual Studio 2010, and I know this feature is available in 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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

我ぃ本無心為│何有愛 2024-12-12 19:47:04

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

  • 在变量更改处设置断点
  • 右键单击断点并选择“条件”
  • 输入条件,如“theNewValue == 42”

现在,只有当条件计算结果为 true 时,断点才会命中。

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

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"

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.NET or any other managed language.

时光瘦了 2024-12-12 19:47:04

只要您使用 Express 以外的 Visual Studio 版本,您就可以使用 断点条件

在“断点条件”对话框的“条件”框中输入有效的表达式,例如myLocalVariable > 1

...如果您想在表达式的值更改时中断,请选择已更改

要访问“已更改”选项,请右键单击“断点”窗口中的断点并选择“条件...”,然后检查下面的屏幕截图。

已更改断点条件选项

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

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

and

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

To get to the Has changed option, right-click your breakpoint in the Breakpoints window and select Condition..., then check the screenshot below.

Has Changed option for breakpoint conditions

猫腻 2024-12-12 19:47:04

使用 F9 添加断点 - 右键单击​​它并选择“条件...” - 现在您可以添加布尔条件,并且只有在该条件计算结果为 true 时才会命中断点。

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.

故事未完 2024-12-12 19:47:04

这取决于断点的范围。如果变量不是本地变量或不是静态变量,您将无法这样做。

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

在此处输入图像描述

选择 条件...

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...

々眼睛长脚气 2024-12-12 19:47:04

您可以使用条件断点。我知道您的问题是针对 VS2010 的,但请注意,从 VS2012 开始,您必须切换到托管兼容模式,才能在 Visual Basic 中使用条件断点。此处描述了原因和方式:

切换到托管兼容模式-in-visual-studio-2013

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:

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

佼人 2024-12-12 19:47:04

VSCode

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

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

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

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

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

  5. 右键单击所需的断点

  6. 选择编辑断点...

  7. 设置条件并按例如:

    myvar == 1234

    myvar 中的“stophere”
    等等

参考文献:

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

VSCode

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

  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

References:

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

守不住的情 2024-12-12 19:47:04

你可以做这两件事。

  1. 在VS中设置断点。右键单击页边空白处的红点,然后选择添加条件。在那里你可以说var==value并选择“Is True”。
  2. 您可以通过上面对话框中的“已更改”选项来实现此目的。

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==value and select "Is True".
  2. You can probably achieve this with the "Has Changed" option in the dialog above.
如若梦似彩虹 2024-12-12 19:47:04

正如其他答案已经指出的那样,条件断点是可能的。正如 JaredPar 所解释的,您可以设置一个断点,右键单击它,选择“条件”并输入您的条件。

自 Visual Studio 2019 Preview 2 起,所谓的“数据断点”可用。在调试模式下,您可以在“自动”或“本地”窗口中选择一个变量,然后右键单击“Break Whan Value Changes”,您可以将其存档。

Microsoft DevBlogs 的这篇文章对此进行了很好的解释: 值更改时中断:Visual Studio 2019 中 .NET Core 的数据断点

Conditional breakpoints are possible as other answers already pointed out. As JaredPar explains you can set a breakpoint, right click on it, select "Conditions" and type your condition(s).

Since Visual Studio 2019 Preview 2 the so called "Data Breakpoints" are availble. While in debugging mode you can select a variable in your "Autos" or "Locals" window and with right click "Break Whan Value Changes" you can archive just that.

This article from Microsoft DevBlogs explains it pretty good: Break When Value Changes: Data Breakpoints for .NET Core in Visual Studio 2019

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文