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);
[<System.Diagnostics.Conditional("TRACE")>]
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方法仅适用于调试版本。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. 可以通过将添加 TraceListener 到集合或从集合中删除一个来自定义此行为 ListenersYou 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. 如果不 DefaultTraceListenerListeners 集合中,则不显示消息框。If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed. DefaultTraceListener可以通过 < > 清除 < 删除 > 或调用 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. 在此文件中,您可以启用和禁用断言消息框或设置 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);
[System.Diagnostics.Conditional("TRACE")]
public static void Assert (bool condition, string message);
[<System.Diagnostics.Conditional("TRACE")>]
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. 如果 type 传入的为 null ,则 Assert 输出一条消息。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方法仅适用于调试版本。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. 可以通过将添加 TraceListener 到集合或从集合中删除一个来自定义此行为 ListenersYou 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. 如果不 DefaultTraceListenerListeners 集合中,则不显示消息框。If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed. DefaultTraceListener可以通过 < > 清除 < 删除 > 或调用 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. 在此文件中,您可以启用和禁用断言消息框或设置 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);
[System.Diagnostics.Conditional("TRACE")]
public static void Assert (bool condition, string message, string detailMessage);
[<System.Diagnostics.Conditional("TRACE")>]
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. 如果 type 传入的为 null ,则 Assert 输出一条消息。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方法仅适用于调试版本。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 ,则它会向集合发送指定的诊断消息和详细消息 ListenersIf the result is false, it sends the specified diagnostic message and detailed message to the Listeners collection. 可以通过将添加 TraceListener 到集合或从集合中删除一个来自定义此行为 ListenersYou 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. 如果不 DefaultTraceListenerListeners 集合中,则不显示消息框。If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed. DefaultTraceListener可以通过 < > 清除 < 删除 > 或调用 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. 在此文件中,您可以启用和禁用断言消息框或设置 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>  

另请参阅

适用于