Contract Contract Contract Contract Class

定义

包含用于表示程序协定(如前置条件、后置条件和对象固定)的静态方法。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
继承
ContractContractContractContract

注解

代码协定类使你可以在代码中指定前置条件、 后置条件和对象不变量。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. 您可以下载二进制重写程序 CCRewrite 从代码约定MSDN DevLabs 网站上。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) Assert(Boolean) Assert(Boolean) Assert(Boolean)

检查条件;如果条件为 false,则遵循为分析器设置的升级策略。Checks for a condition; if the condition is false, follows the escalation policy set for the analyzer.

Assert(Boolean, String) Assert(Boolean, String) Assert(Boolean, String) 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) Assume(Boolean) Assume(Boolean) 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) Assume(Boolean, String) Assume(Boolean, String) 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() EndContractBlock() EndContractBlock() 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) Ensures(Boolean) Ensures(Boolean) Ensures(Boolean)

为封闭方法或属性指定一个后置条件协定。Specifies a postcondition contract for the enclosing method or property.

Ensures(Boolean, String) Ensures(Boolean, String) Ensures(Boolean, String) 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) EnsuresOnThrow<TException>(Boolean) EnsuresOnThrow<TException>(Boolean) EnsuresOnThrow<TException>(Boolean)

基于提供的异常和条件为封闭方法或属性指定一个后置条件协定。Specifies a postcondition contract for the enclosing method or property, based on the provided exception and condition.

EnsuresOnThrow<TException>(Boolean, String) EnsuresOnThrow<TException>(Boolean, String) EnsuresOnThrow<TException>(Boolean, String) 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>) Exists(Int32, Int32, Predicate<Int32>) Exists(Int32, Int32, Predicate<Int32>) 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>) Exists<T>(IEnumerable<T>, Predicate<T>) Exists<T>(IEnumerable<T>, Predicate<T>) Exists<T>(IEnumerable<T>, Predicate<T>)

确定函数中是否存在某个元素集合中的元素。Determines whether an element within a collection of elements exists within a function.

ForAll(Int32, Int32, Predicate<Int32>) ForAll(Int32, Int32, Predicate<Int32>) ForAll(Int32, Int32, Predicate<Int32>) 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>) ForAll<T>(IEnumerable<T>, Predicate<T>) ForAll<T>(IEnumerable<T>, Predicate<T>) ForAll<T>(IEnumerable<T>, Predicate<T>)

确定函数中是否存在某个集合中的所有元素。Determines whether all the elements in a collection exist within a function.

Invariant(Boolean) Invariant(Boolean) Invariant(Boolean) Invariant(Boolean)

为封闭方法或属性指定一个固定的协定。Specifies an invariant contract for the enclosing method or property.

Invariant(Boolean, String) Invariant(Boolean, String) Invariant(Boolean, String) 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) OldValue<T>(T) OldValue<T>(T) OldValue<T>(T)

表示方法或属性开始时的值。Represents values as they were at the start of a method or property.

Requires(Boolean) Requires(Boolean) Requires(Boolean) Requires(Boolean)

为封闭方法或属性指定一个前置条件协定。Specifies a precondition contract for the enclosing method or property.

Requires(Boolean, String) Requires(Boolean, String) Requires(Boolean, String) 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) Requires<TException>(Boolean) Requires<TException>(Boolean) 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) Requires<TException>(Boolean, String) Requires<TException>(Boolean, String) 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>() Result<T>() Result<T>() Result<T>()

表示一个方法或属性的返回值。Represents the return value of a method or property.

ValueAtReturn<T>(T) ValueAtReturn<T>(T) ValueAtReturn<T>(T) ValueAtReturn<T>(T)

表示从一个方法返回时 out 参数的最终(输出)值。Represents the final (output) value of an out parameter when returning from a method.

事件

ContractFailed ContractFailed ContractFailed ContractFailed

协定失败时发生。Occurs when a contract fails.

适用于