void.pointer
void.pointer

Reputation: 26345

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 tool in .NET 3.5 or will I have to implement this myself (if I even can)?

Upvotes: 2

Views: 200

Answers (2)

Ed Bayiates
Ed Bayiates

Reputation: 11210

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

Upvotes: 4

mgronber
mgronber

Reputation: 3419

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

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

Upvotes: 0

Related Questions