Debug.Assert() 仅在单步执行时触发
我正在使用 Visual Studio 2010 编写 .NET 断言:
Debug.Assert(false, "Testing Debug.Assert");
当我在这行代码上放置断点时,等到断点命中,然后让程序继续运行,一切正常:出现“断言失败”对话框弹出。但是,当我删除断点并重新运行应用程序时,Debug.Assert() 语句将被忽略。
有谁知道什么可能会导致这种非常奇怪的行为?
I'm using Visual Studio 2010 to write a .NET assertion:
Debug.Assert(false, "Testing Debug.Assert");
When I put a breakpoint on this line of code, wait until the breakpoint hits, and then let the program continue to run, everything works fine: an "Assertion Failed" dialog pops up. However, when I remove the breakpoint and rerun the application, the Debug.Assert() statement is simply ignored.
Does anyone have an idea what might cause this very strange behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的一位同事立即知道问题出在哪里。我必须在“Visual Studio 2010 /菜单/选项/调试/常规”中启用“仅启用我的代码”标志。显然这是一个 Visual Studio 2010 中的错误"。
A colleague of mine immediately knew what the problem was. I had to enable the "Enable Just My Code" flag at "Visual Studio 2010 / Menu / Options / Debugging / General". Apparently this is a bug in Visual Studio 2010".
我遇到了同样的问题。使用
Trace.Assert
按预期工作。好吧,Trace.Assert 开始在下一个调试会话中表现出相同的行为。
啊,发现问题了。
选中“仅启用我的代码”。
如果在“公共语言运行时异常”中检查“抛出”
“异常”对话框(Ctrl+Alt+E),将抛出断言。
如果未选中,将抛出断言。
未选中“仅启用我的代码”。
如果选中“抛出”,则不会抛出断言。
如果未选中“抛出”,则将抛出断言。
因此,当出现该错误时,不会抛出该 Assert 。未选中“仅启用我的代码”,并检查“公共语言运行时异常”的“抛出”。
I was experiencing the same problem. Using
Trace.Assert
works as expected.Well,
Trace.Assert
started exhibiting the same behavior on the next debugging session.Ah, found the problem.
With 'Enable Just My Code" checked.
If 'Thrown' is checked for 'Common Language Runtime Exceptions' in
the 'Exceptions' dialog (Ctrl+Alt+E), the Assert will be thrown.
If unchecked, the Assert will be thrown.
With 'Enable Just My Code" unchecked.
If 'Thrown' checked, the Assert will not be thrown.
If 'Thrown' unchecked, the Assert will be thrown.
Therefore, the Assert will not be thrown when 'Enable Just My Code' is unchecked and 'Thrown' is checked for 'Common Language Runtime Exceptions'.
这里给出的答案没有帮助,但我发现它就像检查项目构建设置中调试配置的“定义调试常量”选项一样简单:
The answers given here did not help but I found out that it was as simple as checking the "Define DEBUG constant" option for the Debug configuration in the project's build settings: