This topic has not yet been rated - Rate this topic

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

Determines whether the specified object is valid and, if requested, validates all of the properties of the object.

Namespace:  System.ComponentModel.DataAnnotations
Assembly:  System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)
public static bool TryValidateObject(
	Object instance,
	ValidationContext validationContext,
	ICollection<ValidationResult> validationResults,
	bool validateAllProperties
)

Parameters

instance
Type: System.Object
The object to validate.
validationContext
Type: System.ComponentModel.DataAnnotations.ValidationContext
An object that contains information about the validation request.
validationResults
Type: System.Collections.Generic.ICollection<ValidationResult>
A collection to store validation results.
validateAllProperties
Type: System.Boolean
A value that indicates whether all immediate properties of the object are validated.

Return Value

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

instance is null.

ArgumentException

instance does not equal the ObjectInstance on validationContext.

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. If validateAllProperties is set to true, the method validates the property values of the object.

If validationResults is null, the method stops at the first validation failure. If validationResults is not null, 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.

Silverlight

Supported in: 5, 4, 3

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

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
validateAllProperties should be runAllValidationAttributes(besides RequiredAttribute)
Argument validateAllProperties doesn't specify if all properties of the obj should be validated, but if other Attributes than the RequiredAttribute should be executed.  Please refer to the issue report on Microsoft Connect:  http://connect.microsoft.com/VisualStudio/feedback/details/605635/missleading-parametername-validateallproperties-in-validator-try-validate-componentemodel-dataannotations