如何在 C# 中使用 debugbreak()?

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

How can I use debugbreak() in C#?

提问by Prache

What is the syntax and which namespace/class needs to be imported? Give me sample code if possible. It would be of great help.

语法是什么,需要导入哪个命名空间/类?如果可能,请给我示例代码。会有很大帮助。

回答by MagicKat

Put the following where you need it:

将以下内容放在您需要的地方:

System.Diagnostics.Debugger.Break();

回答by Quintin Robinson

you can use System.Diagnostics.Debugger.Break()to break in a specific place.. this can help in situations like debugging a service.

您可以使用System.Diagnostics.Debugger.Break()在特定位置中断..这可以帮助调试服务等情况。

回答by Philip Rieck

I also like to check to see if the debugger is attached - if you call Debugger.Break when there is no debugger, it will prompt the user if they want to attach one. Depending on the behavior you want, you may want to call Debugger.Break() only if (or if not) one is already attached

我还想检查是否附加了调试器——如果在没有调试器的情况下调用 Debugger.Break,它会提示用户是否要附加调试器。根据您想要的行为,您可能希望仅在(或如果没有)已经附加了一个时才调用 Debugger.Break()

using System.Diagnostics;

//.... in the method:

if( Debugger.IsAttached) //or if(!Debugger.IsAttached)
{
  Debugger.Break();
}

回答by CAD bloke

The answers from @Philip Rieck and @John are subtly different.

@Philip Rieck 和 @John 的答案略有不同。

John's ...

约翰的...

#if DEBUG
  System.Diagnostics.Debugger.Break();
#endif

only works if you compiled with the DEBUG conditional compilation symbol set.

仅当您使用 DEBUG 条件编译符号集进行编译时才有效。

Phillip's answer ...

菲利普的回答...

if( Debugger.IsAttached) //or if(!Debugger.IsAttached)
{
  Debugger.Break();
}

will work for any debugger so you will give any hackers a bit of a fright too.

将适用于任何调试器,因此您也会让任何黑客感到害怕。

Also take note of SecurityExceptionit can throw so don't let that code out into the wild.

还要注意SecurityException它可能会抛出,所以不要让该代码暴露在外。

Another reason no to ...

另一个理由不...

If no debugger is attached, users are asked if they want to attach a debugger. If users say yes, the debugger is started. If a debugger is attached, the debugger is signaled with a user breakpoint event, and the debugger suspends execution of the process just as if a debugger breakpoint had been hit.

如果没有附加调试器,系统会询问用户是否要附加调试器。如果用户说是,则启动调试器。如果附加了调试器,调试器会收到用户断点事件的信号,并且调试器会暂停进程的执行,就像命中了调试器断点一样。

from https://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break(v=vs.110).aspx

来自https://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break(v=vs.110).aspx