Debug.Assert 方法

定義

檢查條件;如果條件為 false,則輸出訊息,並顯示訊息方塊,該方塊會顯示呼叫堆疊。Checks for a condition; if the condition is false, outputs messages and displays a message box that shows the call stack.

多載

Assert(Boolean)

檢查條件;如果條件為 false,則會顯示列出呼叫堆疊的訊息方塊。Checks for a condition; if the condition is false, displays a message box that shows the call stack.

Assert(Boolean, String)

檢查條件;如果條件為 false,則會輸出指定的訊息並且顯示列出呼叫堆疊的訊息方塊。Checks for a condition; if the condition is false, outputs a specified message and displays a message box that shows the call stack.

Assert(Boolean, String, String)

檢查條件;如果條件為 false,則輸出兩個指定的訊息,並顯示訊息方塊,該方塊會顯示呼叫堆疊。Checks for a condition; if the condition is false, outputs two specified messages and displays a message box that shows the call stack.

Assert(Boolean, String, String, Object[])

檢查條件;如果條件為 false,則會輸出兩個訊息 (簡單和格式化) 並且顯示列出呼叫堆疊的訊息方塊。Checks for a condition; if the condition is false, outputs two messages (simple and formatted) and displays a message box that shows the call stack.

Assert(Boolean)

檢查條件;如果條件為 false,則會顯示列出呼叫堆疊的訊息方塊。Checks for a condition; if the condition is false, displays a message box that shows the call stack.

public:
 static void Assert(bool condition);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition);
static member Assert : bool -> unit
Public Shared Sub Assert (condition As Boolean)

參數

condition
Boolean

要評估的條件運算式。The conditional expression to evaluate. 如果條件為 true,則不會傳送失敗訊息且不會顯示訊息方塊。If the condition is true, a failure message is not sent and the message box is not displayed.

屬性

範例

下列範例會建立陣列的索引、執行一些動作來設定索引的值, 然後呼叫Assert來確認索引值是否有效。The following example creates an index for an array, performs some action to set the value of the index, and then calls Assert to confirm that the index value is valid. 如果無效, Assert則會輸出呼叫堆疊。If it is not valid, Assert outputs the call stack.

// Create a local value.
int index;

// Perform some action that sets the local value.
index = -40;

// Test that the local value is valid. 
#if defined(DEBUG)
Debug::Assert( index > -1 );
#endif
// Create an index for an array.
 int index;
 
 // Perform some action that sets the index.
 index = -40;
 
 // Test that the index value is valid. 
 Debug.Assert(index > -1);   
' Create an index for an array.
Dim index As Integer

' Perform some action that sets the index.
index = -40

' Test that the index value is valid. 
Debug.Assert((index > - 1))

備註

根據預設, Debug.Assert方法只適用于 debug 組建。By default, the Debug.Assert method works only in debug builds. 如果您想要在發行組建中執行判斷提示, 請使用方法。Trace.AssertUse the Trace.Assert method if you want to do assertions in release builds. 如需詳細資訊,請參閱受控碼中的判斷提示For more information, see Assertions in Managed Code.

重要

Windows 市集Windows Store應用程式無法使用方法。AssertThe Assert methods are not available for Windows 市集Windows Store apps.

一般來說, 方法Assert(Boolean)是用來識別程式開發期間的邏輯錯誤。Typically, the Assert(Boolean) method is used to identify logic errors during program development. Assert評估條件。Assert evaluates the condition. 如果結果為false, 則會將失敗訊息傳送Listeners至集合。If the result is false, it sends a failure message to the Listeners collection. 您可以自訂此行為, 方法TraceListener是在Listeners集合中加入或移除其中一個。You can customize this behavior by adding a TraceListener to, or removing one from, the Listeners collection.

當應用程式在使用者介面模式下執行時,它會顯示訊息方塊,顯示帶有檔案和行號的呼叫堆疊。When the application runs in user interface mode, it displays a message box that shows the call stack with file and line numbers. 訊息方塊包含三個按鈕:[中止]、[重試] 和 [忽略]。The message box contains three buttons: Abort, Retry, and Ignore. 按一下 [中止] 按鈕會終止應用程式。Clicking the Abort button terminates the application. 如果您的應用程式是在偵錯工具中執行, 按一下 [重試] 就會將您傳送至偵錯工具中的程式碼, 如果不是, 則會將它開啟。Clicking Retry sends you to the code in the debugger if your application is running in a debugger, or offers to open a debugger if it is not. 按一下 [過] 會繼續程式碼中的下一個指令。Clicking Ignore continues with the next instruction in the code.

注意

Windows 8.x 市集Windows 8.x Store應用程式不支援強制回應對話方塊, 因此它們在使用者介面模式和非使用者介面模式中的行為相同。apps do not support modal dialog boxes, so they behave the same in user interface mode and non-user interface mode. 訊息會在偵錯工具模式中寫入作用中的追蹤接聽項, 或不會在發行模式中寫入訊息。The message is written to the active trace listeners in debugging mode, or no message is written in release mode.

注意

訊息方塊的顯示取決於是否存在DefaultTraceListenerThe display of the message box depends on the presence of the DefaultTraceListener. DefaultTraceListener如果不Listeners在集合中, 則不會顯示訊息方塊。If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed. DefaultTraceListener可以藉Clear 由<清除> System.Diagnostics.Trace.Listeners.Clear()移除,或在屬性上呼叫方法來移除()。> < ListenersThe DefaultTraceListener can be removed by the <clear>, the <remove>, or by calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()).

您可以DefaultTraceListener在設定檔中變更對應至應用程式名稱的行為。You can change the behavior of the DefaultTraceListener in the configuration file that corresponds to the name of your application. 在此檔案中, 您可以啟用和停用 assert 訊息方塊, 或DefaultTraceListener.LogFileName設定屬性。In this file, you can enable and disable the assert message box or set the DefaultTraceListener.LogFileName property. 設定檔的格式應該如下所示:The configuration file should be formatted as follows:

<configuration>  
  <system.diagnostics>  
    <assert assertuienabled="true" logfilename="c:\\myFile.log" />  
  </system.diagnostics>  
</configuration>  

另請參閱

Assert(Boolean, String)

檢查條件;如果條件為 false,則會輸出指定的訊息並且顯示列出呼叫堆疊的訊息方塊。Checks for a condition; if the condition is false, outputs a specified message and displays a message box that shows the call stack.

public:
 static void Assert(bool condition, System::String ^ message);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition, string message);
static member Assert : bool * string -> unit
Public Shared Sub Assert (condition As Boolean, message As String)

參數

condition
Boolean

要評估的條件運算式。The conditional expression to evaluate. 如果條件為 true,則不會傳送指定的訊息且不會顯示訊息方塊。If the condition is true, the specified message is not sent and the message box is not displayed.

message
String

要傳送給 Listeners 集合的訊息。The message to send to the Listeners collection.

屬性

範例

下列範例會檢查type參數是否有效。The following example checks whether the type parameter is valid. 如果typenull ,Assert則會輸出訊息。If type is null, Assert outputs a message.

void MyMethod( Object^ obj, Type^ type )
{
   #if defined(DEBUG)
   Debug::Assert( type != nullptr, "Type paramater is null" );
   #endif
}
public static void MyMethod(Type type, Type baseType) {
    Debug.Assert(type != null, "Type parameter is null");
 
   // Perform some processing.
 }

Public Shared Sub MyMethod(type As Type, baseType As Type)
    Debug.Assert(Not (type Is Nothing), "Type parameter is null")
End Sub

備註

根據預設, Debug.Assert方法只適用于 debug 組建。By default, the Debug.Assert method works only in debug builds. 如果您想要在發行組建中執行判斷提示, 請使用方法。Trace.AssertUse the Trace.Assert method if you want to do assertions in release builds. 如需詳細資訊,請參閱受控碼中的判斷提示For more information, see Assertions in Managed Code.

一般來說, 方法Assert是用來識別程式開發期間的邏輯錯誤。Typically, the Assert method is used to identify logic errors during program development. Assert評估條件。Assert evaluates the condition. 如果結果為false, 則會將指定的診斷訊息傳送Listeners至集合。If the result is false, it sends the specified diagnostic message to the Listeners collection. 您可以自訂此行為, 方法TraceListener是在Listeners集合中加入或移除其中一個。You can customize this behavior by adding a TraceListener to, or removing one from, the Listeners collection.

當應用程式在使用者介面模式下執行時,它會顯示訊息方塊,顯示帶有檔案和行號的呼叫堆疊。When the application runs in user interface mode, it displays a message box that shows the call stack with file and line numbers. 訊息方塊包含三個按鈕:[中止]、[重試] 和 [忽略]。The message box contains three buttons: Abort, Retry, and Ignore. 按一下 [中止] 按鈕會終止應用程式。Clicking the Abort button terminates the application. 如果您的應用程式是在偵錯工具中執行, 按一下 [重試] 就會將您傳送至偵錯工具中的程式碼, 如果不是, 則會將它開啟。Clicking Retry sends you to the code in the debugger if your application is running in a debugger, or offers to open a debugger if it is not. 按一下 [過] 會繼續程式碼中的下一個指令。Clicking Ignore continues with the next instruction in the code.

注意

訊息方塊的顯示取決於是否存在DefaultTraceListenerThe display of the message box depends on the presence of the DefaultTraceListener. DefaultTraceListener如果不Listeners在集合中, 則不會顯示訊息方塊。If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed. DefaultTraceListener可以藉Clear 由<清除> System.Diagnostics.Trace.Listeners.Clear()移除,或在屬性上呼叫方法來移除()。> < ListenersThe DefaultTraceListener can be removed by the <clear>, the <remove>, or by calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()).

您可以DefaultTraceListener在設定檔中變更對應至應用程式名稱的行為。You can change the behavior of the DefaultTraceListener in the configuration file that corresponds to the name of your application. 在此檔案中, 您可以啟用和停用 assert 訊息方塊, 或DefaultTraceListener.LogFileName設定屬性。In this file, you can enable and disable the assert message box or set the DefaultTraceListener.LogFileName property. 設定檔的格式應該如下所示:The configuration file should be formatted as follows:

<configuration>  
  <system.diagnostics>  
    <assert assertuienabled="true" logfilename="c:\\myFile.log" />  
  </system.diagnostics>  
</configuration>  

另請參閱

Assert(Boolean, String, String)

檢查條件;如果條件為 false,則輸出兩個指定的訊息,並顯示訊息方塊,該方塊會顯示呼叫堆疊。Checks for a condition; if the condition is false, outputs two specified messages and displays a message box that shows the call stack.

public:
 static void Assert(bool condition, System::String ^ message, System::String ^ detailMessage);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition, string message, string detailMessage);
static member Assert : bool * string * string -> unit
Public Shared Sub Assert (condition As Boolean, message As String, detailMessage As String)

參數

condition
Boolean

要評估的條件運算式。The conditional expression to evaluate. 如果條件為 true,就不會傳送指定的訊息,也不會顯示訊息方塊。If the condition is true, the specified messages are not sent and the message box is not displayed.

message
String

要傳送給 Listeners 集合的訊息。The message to send to the Listeners collection.

detailMessage
String

要傳送給 Listeners 集合的詳細訊息。The detailed message to send to the Listeners collection.

屬性

範例

下列範例會檢查type參數是否有效。The following example checks whether the type parameter is valid. 如果typenull, Assert則會輸出兩則訊息。If type is null, Assert outputs two messages.

void MyMethod( Object^ obj, Type^ type )
{
   #if defined(DEBUG)
   Debug::Assert( type != nullptr, "Type paramater is null", "Can't get object for null type" );
   #endif
}
public static void MyMethod(Type type, Type baseType) {
    Debug.Assert(type != null, "Type parameter is null", 
       "Can't get object for null type");
 
   // Perform some processing.
 }

Public Shared Sub MyMethod(type As Type, baseType As Type)
    Debug.Assert( Not (type Is Nothing), "Type parameter is null", "Can't get object for null type")
    ' Perform some processing.
End Sub

備註

根據預設, Debug.Assert方法只適用于 debug 組建。By default, the Debug.Assert method works only in debug builds. 如果您想要在發行組建中執行判斷提示, 請使用方法。Trace.AssertUse the Trace.Assert method if you want to do assertions in release builds. 如需詳細資訊,請參閱受控碼中的判斷提示For more information, see Assertions in Managed Code.

一般來說, 方法Assert(Boolean, String, String)是用來識別程式開發期間的邏輯錯誤。Typically, the Assert(Boolean, String, String) method is used to identify logic errors during program development. Assert評估條件。Assert evaluates the condition. 如果結果為false, 則會將指定的診斷訊息和詳細訊息傳送Listeners至集合。If the result is false, it sends the specified diagnostic message and detailed message to the Listeners collection. 您可以自訂此行為, 方法TraceListener是在Listeners集合中加入或移除其中一個。You can customize this behavior by adding a TraceListener to, or removing one from, the Listeners collection.

當應用程式在使用者介面模式下執行時,它會顯示訊息方塊,顯示帶有檔案和行號的呼叫堆疊。When the application runs in user interface mode, it displays a message box that shows the call stack with file and line numbers. 訊息方塊包含三個按鈕:[中止]、[重試] 和 [忽略]。The message box contains three buttons: Abort, Retry, and Ignore. 按一下 [中止] 按鈕會終止應用程式。Clicking the Abort button terminates the application. 如果您的應用程式是在偵錯工具中執行, 按一下 [重試] 就會將您傳送至偵錯工具中的程式碼, 如果不是, 則會將它開啟。Clicking Retry sends you to the code in the debugger if your application is running in a debugger, or offers to open a debugger if it is not. 按一下 [過] 會繼續程式碼中的下一個指令。Clicking Ignore continues with the next instruction in the code.

注意

訊息方塊的顯示取決於是否存在DefaultTraceListenerThe display of the message box depends on the presence of the DefaultTraceListener. DefaultTraceListener如果不Listeners在集合中, 則不會顯示訊息方塊。If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed. DefaultTraceListener可以藉Clear 由<清除> System.Diagnostics.Trace.Listeners.Clear()移除,或在屬性上呼叫方法來移除()。> < ListenersThe DefaultTraceListener can be removed by the <clear>, the <remove>, or by calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()).

您可以DefaultTraceListener在設定檔中變更對應至應用程式名稱的行為。You can change the behavior of the DefaultTraceListener in the configuration file that corresponds to the name of your application. 在此檔案中, 您可以啟用和停用 assert 訊息方塊, 或DefaultTraceListener.LogFileName設定屬性。In this file, you can enable and disable the assert message box or set the DefaultTraceListener.LogFileName property. 設定檔的格式應該如下所示:The configuration file should be formatted as follows:

<configuration>  
  <system.diagnostics>  
    <assert assertuienabled="true" logfilename="c:\\myFile.log" />  
  </system.diagnostics>  
</configuration>  

另請參閱

Assert(Boolean, String, String, Object[])

檢查條件;如果條件為 false,則會輸出兩個訊息 (簡單和格式化) 並且顯示列出呼叫堆疊的訊息方塊。Checks for a condition; if the condition is false, outputs two messages (simple and formatted) and displays a message box that shows the call stack.

public:
 static void Assert(bool condition, System::String ^ message, System::String ^ detailMessageFormat, ... cli::array <System::Object ^> ^ args);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition, string message, string detailMessageFormat, params object[] args);
static member Assert : bool * string * string * obj[] -> unit
Public Shared Sub Assert (condition As Boolean, message As String, detailMessageFormat As String, ParamArray args As Object())

參數

condition
Boolean

要評估的條件運算式。The conditional expression to evaluate. 如果條件為 true,就不會傳送指定的訊息,也不會顯示訊息方塊。If the condition is true, the specified messages are not sent and the message box is not displayed.

message
String

要傳送給 Listeners 集合的訊息。The message to send to the Listeners collection.

detailMessageFormat
String

要傳送給 Listeners 集合的複合格式字串。The composite format string to send to the Listeners collection. 這個訊息包含混合零個或多個格式項目的文字,而這些格式項目與 args 陣列中的物件相對應。This message contains text intermixed with zero or more format items, which correspond to objects in the args array.

args
Object[]

物件陣列,包含零或多個要格式化的物件。An object array that contains zero or more objects to format.

屬性

備註

這個方法會使用.net 複合格式功能, 將物件的值轉換成其文字表示, 並在字串中內嵌該標記法。This method uses the .NET composite formatting feature to convert the value of an object to its text representation and embed that representation in a string. 產生的字串會傳送至Listeners集合。The resulting string is sent to the Listeners collection.

根據預設, Debug.Assert方法只適用于 debug 組建。By default, the Debug.Assert method works only in debug builds. 如果您想要在發行組建中執行判斷提示, 請使用方法。Trace.AssertUse the Trace.Assert method if you want to do assertions in release builds. 如需詳細資訊,請參閱受控碼中的判斷提示For more information, see Assertions in Managed Code.

一般來說, 方法Assert(Boolean, String, String, Object[])是用來識別程式開發期間的邏輯錯誤。Typically, the Assert(Boolean, String, String, Object[]) method is used to identify logic errors during program development. Assert評估條件。Assert evaluates the condition. 如果false結果為String.Format(String, Object[]) , 則會呼叫方法, 並以detailMessageFormat參數的args形式傳入字串和陣列。If the result is false, The String.Format(String, Object[]) method is called and the detailMessageFormat string and args array are passed in as parameters. Assert(Boolean, String, String, Object[])然後將指定的文字訊息和格式化的文字訊息傳送至Listeners集合。Assert(Boolean, String, String, Object[]) then sends the specified text message and the formatted text message to the Listeners collection. 您可以自訂此行為, 方法TraceListener是在Listeners集合中加入或移除其中一個。You can customize this behavior by adding a TraceListener to, or removing one from, the Listeners collection.

當應用程式在使用者介面模式下執行時,它會顯示訊息方塊,顯示帶有檔案和行號的呼叫堆疊。When the application runs in user interface mode, it displays a message box that shows the call stack with file and line numbers. 訊息方塊包含三個按鈕:[中止]、[重試] 和 [忽略]。The message box contains three buttons: Abort, Retry, and Ignore. 按一下 [中止] 按鈕會終止應用程式。Clicking the Abort button terminates the application. 如果您的應用程式是在偵錯工具中執行, 按一下 [重試] 就會將您傳送至偵錯工具中的程式碼, 如果不是, 則會將它開啟。Clicking Retry sends you to the code in the debugger if your application is running in a debugger, or offers to open a debugger if it is not. 按一下 [過] 會繼續程式碼中的下一個指令。Clicking Ignore continues with the next instruction in the code.

注意

訊息方塊的顯示取決於是否存在DefaultTraceListenerThe display of the message box is dependent on the presence of the DefaultTraceListener. DefaultTraceListener如果不Listeners在集合中, 則不會顯示訊息方塊。If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed. DefaultTraceListener可以藉Clear 由<清除> System.Diagnostics.Trace.Listeners.Clear()移除,或在屬性上呼叫方法來移除()。> < ListenersThe DefaultTraceListener can be removed by the <clear>, the <remove>, or by calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()).

您可以DefaultTraceListener在設定檔中變更對應至應用程式名稱的行為。You can change the behavior of the DefaultTraceListener in the configuration file that corresponds to the name of your application. 在此檔案中, 您可以啟用和停用 assert 訊息方塊, 或DefaultTraceListener.LogFileName設定屬性。In this file, you can enable and disable the assert message box or set the DefaultTraceListener.LogFileName property. 設定檔的格式應該如下所示:The configuration file should be formatted as follows:

<configuration>  
  <system.diagnostics>  
    <assert assertuienabled="true" logfilename="c:\\myFile.log" />  
  </system.diagnostics>  
</configuration>  

適用於