Contract.Requires 方法

定義

指定封入方法或屬性的前置條件合約。Specifies a precondition contract for an enclosing 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.

Requires(Boolean)

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

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

參數

condition
Boolean

要測試的條件運算式。The conditional expression to test.

屬性

備註

  • 這個方法呼叫必須位於方法或屬性的開頭,才會在任何其他程式碼之前。This method call must be at the beginning of a method or property, before any other code.

  • 此合約會向用戶端公開;因此,它必須只參考至少與封入方法相同的成員。This contract is exposed to clients; therefore, it must only reference members that are at least as visible as the enclosing method.

  • 當回溯相容性不會強制您擲回特定的例外狀況時,請使用這個方法,而不是 Contract.Requires<TException>(Boolean) 方法。Use this method instead of the Contract.Requires<TException>(Boolean) method when backward compatibility does not force you to throw a particular exception.

Requires(Boolean, String)

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

public:
 static void Requires(bool condition, System::String ^ userMessage);
[System.Diagnostics.Conditional("CONTRACTS_FULL")]
public static void Requires (bool condition, string userMessage);
static member Requires : bool * string -> unit
Public Shared Sub Requires (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.

屬性

備註

  • 這個方法呼叫必須位於方法或屬性的開頭,才會在任何其他程式碼之前。This method call must be at the beginning of a method or property, before any other code.

  • 此合約會向用戶端公開;因此,它必須只參考至少與封入方法相同的成員。This contract is exposed to clients; therefore, it must only reference members that are at least as visible as the enclosing method.

  • 當回溯相容性不會強制您擲回特定的例外狀況時,請使用這個方法,而不是 Contract.Requires<TException>(Boolean, String) 方法。Use this method instead of the Contract.Requires<TException>(Boolean, String) method when backward compatibility does not force you to throw a particular exception.

Requires<TException>(Boolean)

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

public:
generic <typename TException>
 where TException : Exception static void Requires(bool condition);
public static void Requires<TException> (bool condition) where TException : Exception;
static member Requires : bool -> unit (requires 'Exception :> Exception)
Public Shared Sub Requires(Of TException As Exception) (condition As Boolean)

類型參數

TException

當條件為 false 時所要擲回的例外狀況。The exception to throw if the condition is false.

參數

condition
Boolean

要測試的條件運算式。The conditional expression to test.

備註

重要

您必須開啟執行時間檢查,才能使用 Requires 方法。You must turn on run-time checking to use the Requires method. 如果關閉執行時間檢查,進程將會終止。If run-time checking is turned off, the process will be terminated. 若要取得執行時間檢查的工具,請參閱 MSDN DevLabs 網站上的程式代碼合約To obtain the tools for runtime checking, see Code Contracts on the MSDN DevLabs Web site.

  • 這個方法呼叫必須位於方法或屬性的開頭,才會在任何其他程式碼之前。This method call must be at the beginning of a method or property, before any other code.

  • 此合約會向用戶端公開;因此,它必須只參考至少與封入方法相同的成員。This contract is exposed to clients; therefore, it must only reference members that are at least as visible as the enclosing method.

  • 當您想要在前置條件失敗時擲回例外狀況時,請使用這個方法,而不是 Contract.Requires(Boolean) 方法。Use this method instead of the Contract.Requires(Boolean) method when you want to throw an exception if the precondition 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.

public:
generic <typename TException>
 where TException : Exception static void Requires(bool condition, System::String ^ userMessage);
public static void Requires<TException> (bool condition, string userMessage) where TException : Exception;
static member Requires : bool * string -> unit (requires 'Exception :> Exception)
Public Shared Sub Requires(Of TException As Exception) (condition As Boolean, userMessage As String)

類型參數

TException

當條件為 false 時所要擲回的例外狀況。The exception to throw if the condition is false.

參數

condition
Boolean

要測試的條件運算式。The conditional expression to test.

userMessage
String

如果條件為 false 就會顯示訊息。The message to display if the condition is false.

備註

  • 重要

    您必須開啟執行時間檢查,才能使用 Requires 方法。You must turn on run-time checking to use the Requires method. 如果關閉執行時間檢查,進程將會終止。If run-time checking is turned off, the process will be terminated. 若要取得執行時間檢查的工具,請參閱 MSDN DevLabs 網站上的程式代碼合約To obtain the tools for runtime checking, see Code Contracts on the MSDN DevLabs Web site.

    這個方法呼叫必須位於方法或屬性的開頭,才會在任何其他程式碼之前。This method call must be at the beginning of a method or property, before any other code.

  • 此合約會向用戶端公開;因此,它必須只參考至少與封入方法相同的成員。This contract is exposed to clients; therefore, it must only reference members that are at least as visible as the enclosing method.

  • 當您想要在前置條件失敗時擲回例外狀況時,請使用這個方法,而不是 Contract.Requires(Boolean, String) 方法。Use this method instead of the Contract.Requires(Boolean, String) method when you want to throw an exception if the precondition fails.

適用於