如何测试代码是否在调试模式下运行。
以下是我想要用伪代码实现的内容
if not debugMode then
Do something()
end if
你可以使用Debugger.IsAttached来判断程序是否正在被调试。
If Not Debugger.IsAttached Then
DoSomething()
End If
编辑:如果您始终希望在调试构建中跳过DoSomething
代码,无论是否使用调试器,请使用条件编译和#If,类似于这样的方式。
#IF DEBUG Then
DoSomething()
#End If
您所说的调试模式是指什么?如果您指的是调试构建(debug build),您可以使用#if DEBUG
进行测试:
#if DEBUG
// this is included in a debug build
#else
// this is not included in a debug build
#endif
你可以使用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