当变量在 .NET 中获取特定值时,我可以设置断点吗?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
当然可以设置一个条件,比如变量接收某个值。这称为断点条件。要创建一个,请执行以下操作。
现在,只有当条件计算结果为 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.
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.
只要您使用 Express 以外的 Visual Studio 版本,您就可以使用 断点条件。
和
要访问“已更改”选项,请右键单击“断点”窗口中的断点并选择“条件...”,然后检查下面的屏幕截图。
So long as you are using a Visual Studio edition other than Express, you can achieve this in C# using a breakpoint condition.
and
To get to the Has changed option, right-click your breakpoint in the Breakpoints window and select Condition..., then check the screenshot below.
使用 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.这取决于断点的范围。如果变量不是本地变量或不是静态变量,您将无法这样做。
要设置断点的条件,请右键单击它,您应该会看到以下屏幕:
选择 条件...
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:
Pick Condition...
您可以使用条件断点。我知道您的问题是针对 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
VSCode
在 VisualStudio Code 中,您可以按如下方式设置条件断点:
单击装订线以创建红点断点
从左侧工具栏中选择“调试”(图标:错误上方的圆圈斜杠)
有四个部分:变量、监视、调用堆栈和断点
展开断点部分,以便您可以查看断点
右键单击所需的断点
选择
编辑断点...
设置条件并按。 例如:
myvar == 1234
或
myvar 中的“stophere”
等等
参考文献:
https://code .visualstudio.com/docs/editor/debugging#_conditional-breakpoints
VSCode
In VisualStudio Code, you can set conditional breakpoints as follows:
Click in gutter to create a red-dot breakpoint
Choose Debug from left-side toolbar (icon: circle-slash over bug)
There are four sections: Variables, Watch, Call Stack and Breakpoints
Expand Breakpoints section so you can see the breakpoints
Right-Click on the desired breakpoint
Choose
Edit Breakpoint...
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
你可以做这两件事。
var==value
并选择“Is True”。You can do both of these things.
var==value
and select "Is True".正如其他答案已经指出的那样,条件断点是可能的。正如 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