Debug.Assert
示例代码:
Debug.Assert(logger != null, "Logger should not be null and is ensured by caller."); Debug.Assert(extensionsDirectory != null, "extensionsDirectory should not be null and is ensured by caller.");
检查条件 ;如果条件为 false
,则输出消息,并显示一个消息框,其中显示调用堆栈。
这个断言如果不成功是会弹窗的:
默认情况下, Debug.Assert该方法仅适用于调试版本。 如果要在发布版本中进行断言, 请使用方法。Trace.Assert 有关详细信息,请参阅托管代码中的断言。
重要
这些Assert
方法不适用于Windows 应用商店应用。
通常, 方法Assert(Boolean)用于标识程序开发过程中的逻辑错误。 Assert计算条件。 如果结果为false
, 则它会将失败消息发送到Listeners该集合。 可以通过将添加TraceListener到Listeners集合或从集合中删除一个来自定义此行为。
获取监视调试输出的侦听器集合。
/* Create a listener that outputs to the console screen, and * add it to the debug listeners. */ TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out); Debug.Listeners.Add(myWriter);
侦听器从调试输出生成格式化的输出。 默认情况下, 集合包含DefaultTraceListener类的实例。 若要删除默认侦听器, 请调用Remove方法, 并向其传递的实例。 DefaultTraceListener 若要将输出重定向到控制台窗口, 请添加的ConsoleTraceListener实例。 若要将输出重定向到文件或流, 请添加的TextWriterTraceListener实例。
备注
集合由Debug 和Trace类共享; 将跟踪侦听器添加到任一类会将侦听器添加到这两个类中。 Listeners
原文:https://www.cnblogs.com/Tpf386/p/12111541.html