Assert Método (Boolean, String, String)
Este artículo proviene de un motor de traducción automática. Mueva el puntero sobre las frases del artículo para ver el texto original. Más información.

Trace.Assert (Método) (Boolean, String, String)


Comprueba una condición; si la condición es false, genera dos mensajes especificados y muestra un cuadro de mensaje que muestra la pila de llamadas.

Espacio de nombres:   System.Diagnostics
Ensamblado:  System (en System.dll)

public static void Assert(
	bool condition,
	string message,
	string detailMessage


Type: System.Boolean

Expresión condicional que se va a evaluar. Si la condición es true, no se envían los mensaje especificados y no se muestra el cuadro de mensaje.

Type: System.String

El mensaje que se va a enviar a la colección Listeners.

Type: System.String

El mensaje detallado que se va a enviar a la colección Listeners.

Use the Overload:System.Diagnostics.Trace.Assert method if you want to do assertions in release builds. The Overload:System.Diagnostics.Debug.Assert method works only in debug builds. For more information, see Assertions in Managed Code.

Typically, the M:System.Diagnostics.Trace.Assert(System.Boolean,System.String,System.String) method is used to identify logic errors during program development. M:System.Diagnostics.Trace.Assert(System.Boolean,System.String,System.String) evaluates the condition. If the result is false, it sends the specified diagnostic message and detailed message to the P:System.Diagnostics.Trace.Listeners collection. You can customize this behavior by adding a T:System.Diagnostics.TraceListener to, or removing one from, the P:System.Diagnostics.Trace.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.


The display of the message box depends on the presence of the T:System.Diagnostics.DefaultTraceListener. If the T:System.Diagnostics.DefaultTraceListener is not in the P:System.Diagnostics.Trace.Listeners collection, the message box is not displayed. The T:System.Diagnostics.DefaultTraceListener can be removed by the <clear> Element for <listeners> for <trace>, the <remove> Element for <listeners> for <trace>, or by calling the M:System.Diagnostics.TraceListenerCollection.Clear method on the P:System.Diagnostics.Trace.Listeners property (System.Diagnostics.Trace.Listeners.Clear()).

You can change the behavior of the T:System.Diagnostics.DefaultTraceListener in the configuration file that corresponds to the name of your application. In this file, you can enable and disable the assert message box or set the P:System.Diagnostics.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>

The following example checks to see that the type parameter is valid. If the type passed in is null, the M:System.Diagnostics.Trace.Assert(System.Boolean) outputs a message.

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.

.NET Framework
Disponible desde 1.1
Volver al principio
© 2015 Microsoft