开发者

Is there a version of Debug.Assert() that will check the condition on release?

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 too开发者_如何学编程l in .NET 3.5 or will I have to implement this myself (if I even can)?


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:

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()).

So for example:

#if (!DEBUG)
    System.Diagnostics.Trace.Listeners.Clear();
#endif


Why not simply execute the code and check the result with Debug.Assert?

bool isOk = CodeToBeExecuted();
Debug.Assert(isOk == true);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜