简体   繁体   English

Visual Studio 中的条件断点

[英]Conditional breakpoint in Visual Studio

I want to set a breakpoint on a certain line in C# code when some other variable is equal to a specific value, say:当某些其他变量等于特定值时,我想在 C# 代码的某一行上设置断点,例如:

MyStringVariable == "LKOH"

How can I do that?我怎样才能做到这一点?

I tried to right click on breakpoint icon -> Condition and then typed MyStringVariable == "LKOH" and Visual Studio said it cannot evaluate it.我尝试右键单击断点图标 -> 条件,然后输入MyStringVariable == "LKOH"并且 Visual Studio 说它无法评估它。

if (MyStringVariable == "LKOH") Debugger.Break();

you'll need System.Diagnostics namespace你需要 System.Diagnostics 命名空间

http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break.aspx http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break.aspx

Sample code:示例代码:

static void Main(string[] args) {
  string myvar;
  for (int ix = 0; ix < 10; ++ix) {
    if (ix == 5) myvar = "bar"; else myvar = "foo";
  }    // <=== Set breakpoint here
}

Condition: myvar == "bar"条件:myvar == "bar"

Works well.效果很好。

Just like in code, you need to use:就像在代码中一样,您需要使用:

MyStringVariable == "LKOH"

The double-equals is the key.双等号是关键。 Without it, it's saying it can't evaluate because your expression doesn't evaluate to a boolean.没有它,它表示它无法评估,因为您的表达式不会评估为布尔值。

You should be able to make this work.你应该能够完成这项工作。 Are you using the Exchange instance name in the condition?您是否在条件中使用 Exchange 实例名称? The condition should be something like myExchange.Name == "LKOH" not Exchange.Name == "LKOH" .条件应该类似于myExchange.Name == "LKOH"而不是Exchange.Name == "LKOH"

By the way, using the assignment operator = instead of the equality operator == will work but it will set the property and waste 1/2 hour of your time figuring out what the hell is going on.顺便说一句,使用赋值运算符=而不是相等运算符==会起作用,但它会设置属性并浪费 1/2 小时的时间来弄清楚到底发生了什么。 I made this mistake just yesterday.我昨天刚刚犯了这个错误。

In my case, I forgot that I was debugging a VB application.就我而言,我忘记了我正在调试 VB 应用程序。

In VB equality is = not == like many other languages, thus my conditional breakpoint needed to be myString = "someValue" not myString == "someValue"在 VB 中,等式是= not ==像许多其他语言一样,因此我的条件断点需要是myString = "someValue"而不是myString == "someValue"

The variable you are testing for needs to be in scope at the breakpoint.您正在测试的变量需要在断点处的范围内。

var x = "xxx";
{ 
  var y = "yyy";
}

brak(); // x is in scope, y isn't

For me this made it hit the conditional breakpoint.对我来说,这使它达到了条件断点。

条件断点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在Visual Studio 2017中评估条件断点 - Cannot evaluate conditional breakpoint in Visual Studio 2017 Visual Studio 2015 在条件断点中使用 Linq - Visual Studio 2015 using Linq in conditional breakpoint 条件断点在Visual Studio 2015中不起作用 - Conditional breakpoint not working in Visual Studio 2015 Visual Studio条件断点慢的解决方案或工作单 - Solutions or workaound for Visual Studio conditional breakpoint slowness Visual Studio 2013条件断点无法评估 - Visual Studio 2013 Conditional Breakpoint fails to evaluate Visual Studio:设置条件断点而不先设置无条件断点 - Visual Studio: Setting a conditional breakpoint without setting an unconditional one first 如何使用Visual Studio条件断点打破特定的Guid - How to break on specific Guid using Visual Studio Conditional Breakpoint Visual Studio 断点警告 - Visual Studio Breakpoint Warning Visual Studio 有断点插件吗? - Is there a Breakpoint Plugin for Visual Studio? Visual Studio Community 2015调试器在条件断点处以“不支持本机方法的评估”结束 - 我该如何解决? - Visual Studio Community 2015 debugger ends at conditional breakpoint with “Evaluation of native methods is not supported” - how do I fix?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM