コード例 #1
0
        /// <summary>
        /// Performs data validation on all properties of the provided object.
        /// </summary>
        /// <param name="objectToValidate">The object to validate.</param>
        /// <param name="results">An existing results object (can be useful if you want to add up multiple validations for multiple objects)</param>
        /// <returns>ValidationResults.</returns>
        public static ValidationResults ValidateObject(object objectToValidate, ValidationResults results = null)
        {
            if (results == null)
            {
                results = new ValidationResults {
                    IsValid = true
                }
            }
            ;
            if (objectToValidate == null)
            {
                return(results);
            }

            var objectType = objectToValidate.GetType();
            var properties = objectType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var property in properties.Where(p => p.CanRead))
            {
                var propertyError        = new PropertyValidationResult(objectToValidate, property.Name);
                var validationAttributes = property.GetCustomAttributes(true).OfType <ValidationAttribute>().ToList();
                if (validationAttributes.Count > 0)
                {
                    var propertyValue = property.GetValue(objectToValidate, null);
                    foreach (var validationAttribute in validationAttributes)
                    {
                        var contextObjectAttribute = validationAttribute as IValidationWithContextObject;
                        if (contextObjectAttribute != null)
                        {
                            contextObjectAttribute.ContextObject = objectToValidate;
                        }

                        var realValue = propertyValue != null?propertyValue.ToString() : string.Empty;

                        if (!validationAttribute.IsValid(realValue))
                        {
                            results.IsValid = false;
                            propertyError.ErrorMessages.Add(validationAttribute.FormatErrorMessage(property.Name));
                        }
                    }
                    if (propertyError.ErrorMessages.Count > 0)
                    {
                        results.InvalidProperties.Add(propertyError);
                    }
                }
            }

            return(results);
        }
    }
コード例 #2
0
        /// <summary>
        /// Performs data validation on all properties of the provided object.
        /// </summary>
        /// <param name="objectToValidate">The object to validate.</param>
        /// <param name="results">An existing results object (can be useful if you want to add up multiple valdations for multiple objects)</param>
        /// <returns>ValidationResults.</returns>
        public static ValidationResults ValidateObject(object objectToValidate, ValidationResults results = null)
        {
            if (results == null)
            {
                results = new ValidationResults {
                    IsValid = true
                }
            }
            ;
            if (objectToValidate == null)
            {
                return(results);
            }

            var objectType = objectToValidate.GetType();
            var properties = objectType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);

            foreach (var property in properties)
            {
                var propertyError        = new PropertyValidationResult(objectToValidate, property.Name);
                var propertyValue        = property.GetValue(objectToValidate, null);
                var validationAttributes = property.GetCustomAttributes(true).OfType <ValidationAttribute>().ToList();
                foreach (var validationAttribute in validationAttributes)
                {
                    if (!validationAttribute.IsValid(propertyValue.ToString()))
                    {
                        results.IsValid = false;
                        propertyError.ErrorMessages.Add(validationAttribute.FormatErrorMessage(property.Name));
                    }
                }
                if (propertyError.ErrorMessages.Count > 0)
                {
                    results.InvalidProperties.Add(propertyError);
                }
            }

            return(results);
        }
    }