C# Visual Studio 中的条件断点

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

Conditional breakpoint in Visual Studio

c#visual-studiodebuggingconditional-breakpoint

提问by Captain Comic

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 表示无法对其进行评估。

采纳答案by Hans Passant

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.

效果很好。

回答by David Boike

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.

双等号是关键。没有它,它表示它无法评估,因为您的表达式不会评估为布尔值。

回答by AxelEckenberger

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

回答by Danny G

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

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

回答by Jamie Ide

You should be able to make this work. Are you using the Exchange instance name in the condition? The condition should be something like myExchange.Name == "LKOH"not Exchange.Name == "LKOH".

你应该能够完成这项工作。您是否在条件中使用 Exchange 实例名称?条件应该类似于myExchange.Name == "LKOH"not 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. I made this mistake just yesterday.

顺便说一句,使用赋值运算符=而不是相等运算符==会起作用,但它会设置属性并浪费 1/2 小时的时间来弄清楚到底发生了什么。我昨天刚刚犯了这个错误。

回答by Nick

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中的平等是=不是==像许多其他语言,因此我的条件断点需要的是myString = "someValue"myString == "someValue"