Validator.TryValidateObject Method (Object, ValidationContext, ICollection<ValidationResult>)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Determines whether the specified object is valid.

Namespace:  System.ComponentModel.DataAnnotations
Assembly:  System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)

Syntax

'Declaration
Public Shared Function TryValidateObject ( _
    instance As Object, _
    validationContext As ValidationContext, _
    validationResults As ICollection(Of ValidationResult) _
) As Boolean
public static bool TryValidateObject(
    Object instance,
    ValidationContext validationContext,
    ICollection<ValidationResult> validationResults
)

Parameters

Return Value

Type: System.Boolean
true if the object is valid; otherwise, false.

Exceptions

Exception Condition
ArgumentNullException

instance is nulla null reference (Nothing in Visual Basic).

ArgumentException

instance does not equal the ObjectInstance on validationContext.

Remarks

The TryValidateObject method evaluates each ValidationAttribute attribute that is attached to the object type to determine if the object is valid. The method also checks whether each property that is marked with RequiredAttribute attribute has a value. It does not otherwise validate the property values of the object.If validationResults is nulla null reference (Nothing in Visual Basic), the method stops at the first validation failure. If validationResults is not nulla null reference (Nothing in Visual Basic), the method evaluates all validation attributes and adds all of the failures to validationResults. Class-level validation attributes are evaluated only if the property-level attributes are valid.

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.