Contract 類別

定義

包含表示程式合約的靜態方法,例如前置條件、後置條件及物件非變異。Contains static methods for representing program contracts such as preconditions, postconditions, and object invariants.

public ref class Contract abstract sealed
public static class Contract
type Contract = class
Public Class Contract
繼承
Contract

備註

程式碼合約類別可讓您在程式碼中指定前置條件、後置條件和物件不變數。Code contract classes let you specify preconditions, postconditions, and object invariants in your code. 前置條件是輸入方法或屬性時,必須符合的需求。Preconditions are requirements that must be met when entering a method or property. 後置條件描述在方法或屬性程式碼結束時的期望。Postconditions describe expectations at the time the method or property code exits. 物件非變異性會描述沒有條件問題之類別的預期狀態。Object invariants describe the expected state for a class that has no condition problems. 如需前置條件、後置條件和物件不等變數的詳細資訊,請參閱程式代碼合約For more information about preconditions, postconditions, and object invariants, see Code Contracts.

如需使用程式碼協定的工具和詳細指示,請參閱 MSDN DevLabs 網站上的程式碼合約For tools and detailed instructions for using code contracts, see Code Contracts on the MSDN DevLabs Web site.

重要

您必須使用二進位重寫器來插入合約的執行時間強制執行。You must use a binary rewriter to insert run-time enforcement of contracts. 否則,例如方法的Contract.Ensures合約只能以靜態方式測試,而且如果違反合約,就不會在執行時間擲回例外狀況。Otherwise, contracts such as the Contract.Ensures method can only be tested statically and will not throw exceptions during run time if a contract is violated. 您可以從 MSDN DevLabs 網站上的程式代碼合約下載二進位重寫工具 CCRewrite。You can download the binary rewriter CCRewrite from Code Contracts on the MSDN DevLabs Web site. CCRewrite 隨附 Visual Studio 增益集,可讓您從專案屬性頁面啟動執行時間合約強制執行。CCRewrite comes with a Visual Studio add-in that enables you to activate run-time contract enforcement from the project Properties page. 二進位重寫工具和 Visual Studio 增益集不會Visual Studio 2010Visual Studio 2010隨附或 Windows SDK。The binary rewriter and the Visual Studio add-in do not ship with Visual Studio 2010Visual Studio 2010 or the Windows SDK.

方法

Assert(Boolean)

檢查條件,且如果條件為 false,則遵循已為分析器設定的擴大原則。Checks for a condition; if the condition is false, follows the escalation policy set for the analyzer.

Assert(Boolean, String)

檢查條件。如果條件為 false,則遵循分析器所設定的擴大原則,並顯示指定的訊息。Checks for a condition; if the condition is false, follows the escalation policy set by the analyzer and displays the specified message.

Assume(Boolean)

指示程式碼分析工具假設指定的條件為 true,即使該條件無法以靜態方式證明永遠是 true 亦然。Instructs code analysis tools to assume that the specified condition is true, even if it cannot be statically proven to always be true.

Assume(Boolean, String)

指示程式碼分析工具假設條件為 true,即使該條件無法以靜態方式證明永遠是 true 亦然,同時在假設失敗時顯示訊息。Instructs code analysis tools to assume that a condition is true, even if it cannot be statically proven to always be true, and displays a message if the assumption fails.

EndContractBlock()

當方法的合約僅包含 if-then-throw 形式的前置條件時,標記合約區段的結尾。Marks the end of the contract section when a method's contracts contain only preconditions in the if-then-throw form.

Ensures(Boolean)

指定封入方法或屬性的後置條件合約。Specifies a postcondition contract for the enclosing method or property.

Ensures(Boolean, String)

指定提供之結束條件的後置條件合約,以及在條件為 false 時所要顯示的訊息。Specifies a postcondition contract for a provided exit condition and a message to display if the condition is false.

EnsuresOnThrow<TException>(Boolean)

根據所提供的例外狀況和條件,針對封入的方法或屬性指定後置條件合約。Specifies a postcondition contract for the enclosing method or property, based on the provided exception and condition.

EnsuresOnThrow<TException>(Boolean, String)

針對封入方法或屬性來指定後置條件合約,和當條件是 false 時,根據所提供之例外狀況和條件來顯示的訊息。Specifies a postcondition contract and a message to display if the condition is false for the enclosing method or property, based on the provided exception and condition.

Exists(Int32, Int32, Predicate<Int32>)

判斷整數範圍內是否有任何整數使指定的測試成立。Determines whether a specified test is true for any integer within a range of integers.

Exists<T>(IEnumerable<T>, Predicate<T>)

判斷項目集合內的項目是否出現在函式中。Determines whether an element within a collection of elements exists within a function.

ForAll(Int32, Int32, Predicate<Int32>)

判斷特定條件對於指定之範圍內的所有整數是否有效。Determines whether a particular condition is valid for all integers in a specified range.

ForAll<T>(IEnumerable<T>, Predicate<T>)

判斷集合內的所有項目是否都出現在函式中。Determines whether all the elements in a collection exist within a function.

Invariant(Boolean)

指定封入方法或屬性的非變異合約。Specifies an invariant contract for the enclosing method or property.

Invariant(Boolean, String)

指定封入方法或屬性的非變異合約,並在合約的條件失敗時顯示訊息。Specifies an invariant contract for the enclosing method or property, and displays a message if the condition for the contract fails.

OldValue<T>(T)

表示其在方法或屬性開始時的值。Represents values as they were at the start of a method or property.

Requires(Boolean)

指定封入方法或屬性的前置條件合約。Specifies a precondition contract for the enclosing method or property.

Requires(Boolean, String)

指定封入方法或屬性的前置條件合約,並在合約的條件失敗時顯示訊息。Specifies a precondition contract for the enclosing method or property, and displays a message if the condition for the contract fails.

Requires<TException>(Boolean)

指定封入方法或屬性的前置條件合約,並在合約的條件失敗時擲回例外狀況。Specifies a precondition contract for the enclosing method or property, and throws an exception if the condition for the contract fails.

Requires<TException>(Boolean, String)

指定封入方法或屬性的前置條件合約,並在合約的條件失敗時擲回包含所提供訊息的例外狀況。Specifies a precondition contract for the enclosing method or property, and throws an exception with the provided message if the condition for the contract fails.

Result<T>()

表示方法或屬性的傳回值。Represents the return value of a method or property.

ValueAtReturn<T>(T)

表示從方法傳回時的最後 (輸出) out 參數值。Represents the final (output) value of an out parameter when returning from a method.

事件

ContractFailed

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

適用於