Contract.ContractFailed 事件

定義

發生於合約失敗時。Occurs when a contract fails.

public:
 static event EventHandler<System::Diagnostics::Contracts::ContractFailedEventArgs ^> ^ ContractFailed;
[add: System.Security.SecurityCritical]
[remove: System.Security.SecurityCritical]
public static event EventHandler<System.Diagnostics.Contracts.ContractFailedEventArgs> ContractFailed;
member this.ContractFailed : EventHandler<System.Diagnostics.Contracts.ContractFailedEventArgs> 
Public Shared Custom Event ContractFailed As EventHandler(Of ContractFailedEventArgs) 
屬性

備註

這個事件的事件資訊是由傳遞至事件處理常式的 System.Diagnostics.Contracts.ContractFailedEventArgs 物件所提供。The event information for this event is supplied by the System.Diagnostics.Contracts.ContractFailedEventArgs object that is passed to the event handler. 此事件會通知受管理的應用程式環境,例如合約失敗的互動式解譯器或 Web 瀏覽器主機。This event notifies a managed application environment such as an interactive interpreter or a Web browser host that a contract has failed. 在這麼做之前,它會引發事件,讓應用程式可以視情況進行處理。Before doing so, it will raise an event that an application can handle as it sees fit. 例如,如果程式碼是在測試架構中執行,您可以記錄測試失敗,然後結束測試。For example, if code is being run in a testing framework, you can log a test failure and then end the test.

.NET Framework Class Library 中 RaiseContractFailedEvent 方法的預設執行是呼叫每個向 ContractFailed 事件註冊的處理常式。The default implementation of the RaiseContractFailedEvent method in the .NET Framework class library is to call each handler that is registered with the ContractFailed event. 系統會忽略處理常式擲回的例外狀況,但每個處理常式都可以藉由呼叫事件引數的 SetHandled 方法,指出是否處理失敗。Exceptions thrown by handlers are ignored, but each handler can indicate whether the failure is handled by calling the SetHandled method for the event arguments. 如果有任何處理程式將失敗設定為已處理,則方法會傳回 null,而不會採取進一步的動作。If any handler sets the failure as handled, the method returns null and no further action is taken. 或者,處理常式也可以呼叫 SetUnwind 方法,指示程式碼進行回溯。Alternatively, handlers can call the SetUnwind method to instruct the code to unwind. 在此情況下,執行所有處理常式之後,就會擲回例外狀況。In that case, an exception is thrown after all handlers have executed.

安全性

SecurityCriticalAttribute
需要完全信任立即呼叫者。requires full trust for the immediate caller. 部分信任或透明的程式碼無法使用這個成員。This member cannot be used by partially trusted or transparent code.

適用於