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.
コードコントラクトを使用するためのツールと詳細な手順については、Visual Studio Marketplace の コードコントラクト に関する記述を参照してください。For tools and detailed instructions for using code contracts, see Code Contracts on the Visual Studio Marketplace.
重要
バイナリリライターを使用して、コントラクトの実行時の適用を挿入する必要があります。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. Visual Studio Marketplace で コードコントラクト からバイナリリライター CCRewrite をダウンロードできます。You can download the binary rewriter CCRewrite from Code Contracts on the Visual Studio Marketplace. 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) |
条件をチェックし、その条件が |
Assert(Boolean, String) |
条件をチェックし、その条件が |
Assume(Boolean) |
常に |
Assume(Boolean, String) |
常に |
EndContractBlock() |
メソッドのコントラクトに |
Ensures(Boolean) |
外側のメソッドまたはプロパティの実行後の状態のコントラクトを指定します。Specifies a postcondition contract for the enclosing method or property. |
Ensures(Boolean, String) |
指定された終了条件の実行後の状態のコントラクトと、条件が |
EnsuresOnThrow<TException>(Boolean) |
指定された例外および条件に基づいて、外側のメソッドまたはプロパティに実行後の状態のコントラクトを指定します。Specifies a postcondition contract for the enclosing method or property, based on the provided exception and condition. |
EnsuresOnThrow<TException>(Boolean, String) |
指定された例外および条件に基づいて、実行後の状態のコントラクトと、外側のメソッドまたはプロパティの条件が |
Exists(Int32, Int32, Predicate<Int32>) |
ある整数範囲内のすべての整数について指定したテストが true かどうかを判定します。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) |
メソッドから戻るときの |
イベント
ContractFailed |
コントラクトが失敗すると発生します。Occurs when a contract fails. |