是否有一个版本Debug.Assert的(),将检查情况的​​释放?有一个、版本、情况、Debug

2023-09-06 05:57:41 作者:原来你只是路过

我需要Debug.Assert的版本(),在发布版本,仍然会执行code的条件参数,但如果断言失败将不会显示断言对话框。有没有在.NET 3.5这样的工具,或者将要实现这一点我自己(如果我甚至可以)?

I need a version of Debug.Assert() that, in a release build, will still execute the code in the condition parameter but will not show an assertion dialog if the assertion fails. Is there such a tool in .NET 3.5 or will I have to implement this myself (if I even can)?

推荐答案

使用的 Trace.Assert 对于这一点,它工作在释放模式也是如此。请参阅如何使用监听器使用其他方法不是使想出一个对话框的文档。摘录:

Use Trace.Assert for this, it works in release mode as well. See the documentation on how to use listeners to use another method than making a dialog come up. An excerpt:

消息框的显示取决于DefaultTraceListener的presence。如果DefaultTraceListener不在监听器集合,不显示消息框。该DefaultTraceListener可以通过删除的<清除>元素<听众>为<跟踪>中的<删除>元素<听众>为<跟踪>中或通过调用监听器属性的清除方法(System.Diagnostics.Trace.Listeners.Clear())

The display of the message box depends on the presence of the DefaultTraceListener. If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed. The DefaultTraceListener can be removed by the <clear> Element for <listeners> for <trace>, the <remove> Element for <listeners> for <trace>, or by calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()).

因此​​,例如:

#if (!DEBUG)
    System.Diagnostics.Trace.Listeners.Clear();
#endif
 
精彩推荐
图片推荐