测试代码是否在调试模式下执行

问题描述 投票:13回答:3

如何测试代码在调试模式下执行。

这是我想在伪代码中做的事情

if not debugMode then
  Do something()
end if
vb.net debugging
3个回答
22
投票

您可以使用Debugger.IsAttached来确定程序是否正在被调试。

If Not Debugger.IsAttached Then
  DoSomething()
End If

编辑如果您总是想在调试版本中跳过DoSomething代码,无论是否使用调试器,请使用conditional compilation#If,类似这样的

#IF DEBUG Then
  DoSomething()
#End If

10
投票

调试模式是什么意思?如果您参考调试版本,可以使用#if DEBUG来测试:

#if DEBUG
    // this is included in a debug build
#else
    // this is not included in a debug build
#endif

1
投票

你可以使用IsDebuggerPresent函数

<DllImport("kernel32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function IsDebuggerPresent() As Boolean
End Function

if not isDebuggerPresent() then
Do something()
end if
© www.soinside.com 2019 - 2024. All rights reserved.