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);
public static void Assert (bool condition);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition);
static member Assert : bool -> unit
[<System.Diagnostics.Conditional("DEBUG")>]
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 方法仅适用于调试版本。By default, the Debug.Assert method works only in debug builds. Trace.Assert如果要在发布版本中进行断言,请使用方法。Use the Trace.Assert method if you want to do assertions in release builds. 有关详细信息,请参阅托管代码中的断言For more information, see Assertions in Managed Code.

重要

这些 Assert 方法不适用于 Windows 应用商店Windows Store 应用。The 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 到集合或从集合中删除一个来自定义此行为 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.

备注

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. 如果不 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>  
    <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);
public static void Assert (bool condition, string message);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition, string message);
static member Assert : bool * string -> unit
[<System.Diagnostics.Conditional("DEBUG")>]
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 方法仅适用于调试版本。By default, the Debug.Assert method works only in debug builds. Trace.Assert如果要在发布版本中进行断言,请使用方法。Use 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 到集合或从集合中删除一个来自定义此行为 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>  
    <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);
public static void Assert (bool condition, string message, string detailMessage);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition, string message, string detailMessage);
static member Assert : bool * string * string -> unit
[<System.Diagnostics.Conditional("DEBUG")>]
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 方法仅适用于调试版本。By default, the Debug.Assert method works only in debug builds. Trace.Assert如果要在发布版本中进行断言,请使用方法。Use 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 ,则它会向集合发送指定的诊断消息和详细消息 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>  
    <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);
public static void Assert (bool condition, string message, string detailMessageFormat, params object[] args);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition, string? message, string detailMessageFormat, params 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
[<System.Diagnostics.Conditional("DEBUG")>]
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 方法仅适用于调试版本。By default, the Debug.Assert method works only in debug builds. Trace.Assert如果要在发布版本中进行断言,请使用方法。Use 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 到集合或从集合中删除一个来自定义此行为 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 is dependent 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>  
    <assert assertuienabled="true" logfilename="c:\\myFile.log" />  
  </system.diagnostics>  
</configuration>  

适用于