CustomValidationAttribute.Method 屬性


取得驗證方法。Gets the validation method.

 property System::String ^ Method { System::String ^ get(); };
public string Method { get; }
member this.Method : string
Public ReadOnly Property Method As String



驗證方法的名稱。The name of the validation method.


這個屬性所指定的自訂方法必須是 public 和 static,而且必須傳回布林值。The custom method specified by this property must be public and static, and it must return a Boolean value. 它也必須至少要有一個輸入參數,以指定要驗證的物件。It must also take at least one input parameter that specifies the object to validate. 這個參數可以是強型別。This parameter can be strongly typed. 如果進程傳遞不同類型的值,則會嘗試型別轉換。If a process passes a value of a different type, type conversion will be attempted.

如果指定的物件有效,則指定的方法應該會 true 傳回。The specified method should return true if a given object is valid. 否則應該傳回 falseOtherwise it should return false.

自訂方法也可以取得指定 ValidationContext 輸入值和輸出值的參數 ValidationResultThe custom method can also take parameters that specify a ValidationContext input value and a ValidationResult output value. ValidationContext參數會提供額外的內容資訊,方法可以使用此資訊來判斷所用的內容。The ValidationContext parameter provides additional context information that the method can use to determine the context that it is used in. ValidationResultOutput 參數可讓方法傳回錯誤訊息。The ValidationResult output parameter enables the method to return an error message.

如果方法 null 針對 ValidationResult 參數傳回,或傳回空白值給 ErrorMessage 屬性,則 FormatErrorMessage 會呼叫預設方法來撰寫錯誤訊息。If the method returns null for the ValidationResult parameter or if it returns an empty value for the ErrorMessage property, the default FormatErrorMessage method will be called to compose the error message.