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

檢查條件;如果條件為 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("TRACE")]
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.

屬性

範例

下列範例會建立陣列的索引。The following example creates an index for an array. 接著會執行一些動作來設定索引的值。Then some action is performed that sets the value of the index. 接下來,程式碼會呼叫 Assert 來確認索引值是否有效。Next the code calls Assert to verify the index value is valid. 如果無效,Assert 會輸出呼叫堆疊。If it is not valid, the Assert outputs the call stack.

protected:
   // Create an index for an array.
   int index;

   void Method()
   {
      // Perform some action that sets the index.
      // Test that the index value is valid.
      #if defined(TRACE)
      Trace::Assert( index > -1 );
      #endif
   }
// Create an index for an array.
 protected int index;
 
 protected void Method()
 { 
 // Perform some action that sets the index.
 
 // Test that the index value is valid. 
 Trace.Assert(index > -1);
 }  
' Create an index for an array.
Protected index As Integer    

Protected Sub Method()
    ' Perform some action that sets the index.
    ' Test that the index value is valid. 
    Trace.Assert(index > -1)
End Sub

備註

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

一般來說,Assert(Boolean) 方法是用來識別程式開發期間的邏輯錯誤。Typically, the Assert(Boolean) method is used to identify logic errors during program development. Assert(Boolean) 會評估條件。Assert(Boolean) evaluates the condition. 如果結果是 false,它會將失敗訊息傳送至 Listeners 集合。If the result is false, it sends a failure message to the Listeners collection. 您可以自訂此行為,方法是在 Listeners 集合中加入或移除 TraceListenerYou 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.

注意

訊息方塊的顯示取決於 DefaultTraceListener是否存在。The 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><移除>或在 Clear 屬性(Listeners)上呼叫System.Diagnostics.Trace.Listeners.Clear()方法來移除。The 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>  
    <switches>  
      <add name="mySwitch" value="4"/>  
    </switches>  
    <trace autoflush="false" indentsize="4"/>  
    <assert assertuienabled="true" logfilename=".\TraceLog.txt"/>  
  </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("TRACE")]
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 to see that the type parameter is valid. 如果傳入的 typenullAssert 就會輸出訊息。If the type passed in is null, the Assert outputs a message.

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

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

    ' Perform some processing.
End Sub

備註

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

一般來說,Assert(Boolean, String) 方法是用來識別程式開發期間的邏輯錯誤。Typically, the Assert(Boolean, String) method is used to identify logic errors during program development. Assert(Boolean, String) 會評估條件。Assert(Boolean, String) evaluates the condition. 如果結果是 false,它會將指定的診斷訊息傳送至 Listeners 集合。If the result is false, it sends the specified diagnostic message to the Listeners collection. 您可以自訂此行為,方法是在 Listeners 集合中加入或移除 TraceListenerYou 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.

注意

訊息方塊的顯示取決於 DefaultTraceListener是否存在。The 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><移除>或在 Clear 屬性(Listeners)上呼叫System.Diagnostics.Trace.Listeners.Clear()方法來移除。The 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>  
    <switches>  
      <add name="mySwitch" value="4"/>  
    </switches>  
    <trace autoflush="false" indentsize="4"/>  
    <assert assertuienabled="true" logfilename=".\TraceLog.txt"/>  
  </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("TRACE")]
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 to see that the type parameter is valid. 如果傳入的 typenullAssert 就會輸出訊息。If the type passed in is null, the Assert outputs a message.

public:
   static void MyMethod( Type^ type, Type^ baseType )
   {
      #if defined(TRACE)
      Trace::Assert( type != nullptr, "Type parameter is null", "Can't get object for null type" );
      #endif
      
      // Perform some processing.
   }
public static void MyMethod(Type type, Type baseType) {
    Trace.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)
    Trace.Assert( Not (type Is Nothing), "Type parameter is null", _
        "Can't get object for null type")

    ' Perform some processing.
End Sub

備註

如果您想要在發行組建中執行判斷提示,請使用 Trace.Assert 方法。Use the Trace.Assert method if you want to do assertions in release builds. Debug.Assert 方法只適用于 debug 組建。The Debug.Assert method works only in debug 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. 您可以自訂此行為,方法是在 Listeners 集合中加入或移除 TraceListenerYou 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.

注意

訊息方塊的顯示取決於 DefaultTraceListener是否存在。The 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><移除>或在 Clear 屬性(Listeners)上呼叫System.Diagnostics.Trace.Listeners.Clear()方法來移除。The 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>  
    <switches>  
      <add name="mySwitch" value="4"/>  
    </switches>  
    <trace autoflush="false" indentsize="4"/>  
    <assert assertuienabled="true" logfilename=".\TraceLog.txt"/>  
  </system.diagnostics>  
</configuration>  

另請參閱

適用於