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集合或從集合中刪除一個來自定義此行為。
Debug.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