ValidationAttribute.Validate 方法

定義

驗證指定的物件。Validates the specified object.

多載

Validate(Object, ValidationContext)

驗證指定的物件。Validates the specified object.

Validate(Object, String)

驗證指定的物件。Validates the specified object.

Validate(Object, ValidationContext)

驗證指定的物件。Validates the specified object.

public:
 void Validate(System::Object ^ value, System::ComponentModel::DataAnnotations::ValidationContext ^ validationContext);
public void Validate (object value, System.ComponentModel.DataAnnotations.ValidationContext validationContext);
public void Validate (object? value, System.ComponentModel.DataAnnotations.ValidationContext validationContext);
member this.Validate : obj * System.ComponentModel.DataAnnotations.ValidationContext -> unit
Public Sub Validate (value As Object, validationContext As ValidationContext)

參數

value
Object

要驗證的物件。The object to validate.

validationContext
ValidationContext

ValidationContext 物件,該物件描述會在其中執行驗證檢查的內容。The ValidationContext object that describes the context where the validation checks are performed. 這個參數不可以是 nullThis parameter cannot be null.

例外狀況

驗證失敗。Validation failed.

目前的屬性格式不正確。The current attribute is malformed.

衍生類別尚未實作 IsValid(Object, ValidationContext)IsValid(Object, ValidationContext) has not been implemented by a derived class.

備註

這個方法會呼叫 IsValid 方法來判斷參數所指定的物件是否 value 有效。This method calls the IsValid method to determine whether the object that is specified by the value parameter is valid. 如果 IsValid 方法傳回 false ,這個方法會呼叫方法, FormatErrorMessage 以取得指出問題的當地語系化訊息。If the IsValid method returns false, this method calls the FormatErrorMessage method to get a localized message that states the problem. 然後,它會擲回 ValidationException 例外狀況。It then throws a ValidationException exception.

適用於

Validate(Object, String)

驗證指定的物件。Validates the specified object.

public:
 void Validate(System::Object ^ value, System::String ^ name);
public void Validate (object value, string name);
public void Validate (object? value, string name);
member this.Validate : obj * string -> unit
Public Sub Validate (value As Object, name As String)

參數

value
Object

要驗證的物件值。The value of the object to validate.

name
String

要包含在錯誤訊息中的名稱。The name to include in the error message.

例外狀況

value 無效。value is not valid.

目前的屬性格式不正確。The current attribute is malformed.

備註

這個方法會驗證宣告屬性之物件或屬性的值 ValidationAttributeThis method validates the value of the object or property on which the ValidationAttribute attribute is declared. 它會呼叫 IsValid 方法,並傳遞 value 做為參數。It calls the IsValid method and passes value as a parameter. 如果驗證失敗,則 ValidationException 會擲回例外狀況。If validation fails, a ValidationException exception is thrown.

適用於