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);
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 FULL 符號是否存在,有條件地加以定義。Invariants 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);
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 FULL 符號上。Invariants 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.

適用於