Contract.Invariant 方法

定义

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

重载

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.

Invariant(Boolean)

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

public:
 static void Invariant(bool condition);
[System.Diagnostics.Conditional("CONTRACTS_FULL")]
public static void Invariant (bool condition);
[<System.Diagnostics.Conditional("CONTRACTS_FULL")>]
static member Invariant : bool -> unit
Public Shared Sub Invariant (condition As Boolean)

参数

condition
Boolean

要测试的条件表达式。The conditional expression to test.

属性

注解

Invariant 协定包含在由特性标识的方法中 ContractInvariantMethodAttribute ; 通常,方法名为 ObjectInvariantInvariant contracts are contained within a method that is identified by the ContractInvariantMethodAttribute attribute; typically, the method is named ObjectInvariant.

  • 只能在类上声明的专用固定方法中指定该协定。This contract can be specified only in a dedicated invariant method that is declared on a class. 如果该方法不是密封的,则它应只引用受保护的成员而不是私有成员,这样子类就可以确保保持固定不变。If the method is not sealed, it should refer only to protected members, not private members, so that subclasses can be sure to maintain the invariants.

  • 此协定未向客户端公开;因此,它可能引用与封闭方法不可见的成员。This contract is not exposed to clients; therefore, it may reference members that are less visible than the enclosing method.

  • 必须使用二进制重写程序来执行此固定的运行时强制。You must use the binary rewriter for run-time enforcement of this invariant.

  • 固定条件根据符号是否存在有条件地定义 CONTRACTS FULLInvariants are conditionally defined based on the presence of the CONTRACTS FULL symbol. 在运行时检查期间,每次公共方法结束都要检查固定协定。During run-time checking, invariants are checked at the end of each public method. 如果某个固定条件提到同一个类中的公共方法,则在该公共方法结束时通常会发生的固定检查将被禁用,并且仅在对该类的最外面的方法调用结束时检查。If an invariant mentions a public method in the same class, the invariant check that would normally happen at the end of that public method is disabled and checked only at the end of the outermost method call to that class. 如果因调用其他类上的方法而重新输入类,也会发生此类情况。This also happens if the class is re-entered because of a call to a method on another class.

适用于

Invariant(Boolean, String)

为封闭方法或属性指定一个固定协定,并在该协定的条件失败时显示一条消息。Specifies an invariant contract for the enclosing method or property, and displays a message if the condition for the contract fails.

public:
 static void Invariant(bool condition, System::String ^ userMessage);
[System.Diagnostics.Conditional("CONTRACTS_FULL")]
public static void Invariant (bool condition, string userMessage);
[System.Diagnostics.Conditional("CONTRACTS_FULL")]
public static void Invariant (bool condition, string? userMessage);
[<System.Diagnostics.Conditional("CONTRACTS_FULL")>]
static member Invariant : bool * string -> unit
Public Shared Sub Invariant (condition As Boolean, userMessage As String)

参数

condition
Boolean

要测试的条件表达式。The conditional expression to test.

userMessage
String

条件为 false 时要显示的消息。The message to display if the condition is false.

属性

注解

Invariant 协定包含在由特性标识的方法中 ContractInvariantMethodAttribute ; 通常,方法名为 ObjectInvariantInvariant contracts are contained within a method that is identified by the ContractInvariantMethodAttribute attribute; typically, the method is named ObjectInvariant.

  • 只能在类上声明的专用固定方法中指定该协定。This contract can be specified only in a dedicated invariant method that is declared on a class.

  • 此协定未向客户端公开;因此,它可能引用与封闭方法不可见的成员。This contract is not exposed to clients; therefore, it may reference members that are less visible than the enclosing method.

  • 必须使用二进制重写程序来执行此固定的运行时强制。You must use the binary rewriter for run-time enforcement of this invariant.

  • 固定条件是在符号上定义的 CONTRACTS FULLInvariants are conditionally defined on the CONTRACTS FULL symbol. 在运行时检查期间,每次公共方法结束都要检查固定协定。During run-time checking, invariants are checked at the end of each public method. 如果某个固定条件提到同一个类中的公共方法,则在该公共方法结束时通常会发生的固定检查将被禁用,并且仅在对该类的最外面的方法调用结束时检查。If an invariant mentions a public method in the same class, the invariant check that would normally happen at the end of that public method is disabled and checked only at the end of the outermost method call to that class. 如果因调用其他类上的方法而重新输入类,也会发生此类情况。This also happens if the class is re-entered because of a call to a method on another class.

适用于