예제 #1
0
        /// <summary>
        /// The method validates whether an object is valid.
        /// </summary>
        /// <returns>True - if object is valid, false - if object is invalid.</returns>
        public override bool IsValid()
        {
            bool        result            = false;
            List <bool> validationResults = new List <bool>();

            PropertyInfo[] pi = objectToValidate.GetType().GetProperties();

            foreach (PropertyInfo pInfo in pi)
            {
                object[] attributes = pInfo.GetCustomAttributes(typeof(ValidationAttributeBase), false);

                foreach (var attribute in attributes)
                {
                    ValidationAttributeBase attributeToBeUsed = attribute as ValidationAttributeBase;
                    if (attributeToBeUsed != null)
                    {
                        bool validationIntermediaryResult = attributeToBeUsed.Validate(pInfo.GetValue(objectToValidate));

                        validationResults.Add(validationIntermediaryResult);

                        if (!validationIntermediaryResult)
                        {
                            ErrorMessage.Append(attributeToBeUsed.GetErrorMessage());
                            ErrorMessage.AppendLine();
                        }
                    }
                }
            }

            result = validationResults.All(x => x == true);

            return(result);
        }
예제 #2
0
        /// <summary>
        /// validate a property
        /// </summary>
        /// <param name="model"></param>
        /// <param name="prop"></param>
        /// <returns></returns>
        private static List <ValidationResult> Validate(object model, PropertyInfo prop)
        {
            try
            {
                //get property value
                object value = prop.GetValue(model, null);

                if (model.GetType().IsGenericType || model.GetType().IsArray)
                {
                    if (Attribute.IsDefined(prop, typeof(Validate)))
                    {
                        //validate array/list object
                        return(ValidateArray(prop.GetValue(model, null)));
                    }
                }

                if (!value.GetType().IsValueType&& value.GetType() != typeof(string))
                {
                    //if reference type property need validate
                    if (Attribute.IsDefined(prop, typeof(Validate)))
                    {
                        //call validate on value object
                        return(Validate(value));
                    }
                }

                //list validation result to return
                List <ValidationResult> lstResult = new List <ValidationResult>();

                //get all attributes associate with property
                object[] attributes = prop.GetCustomAttributes(true);

                foreach (object attr in attributes)
                {
                    //try cast attribute to type of validation attribute base
                    ValidationAttributeBase attribute = attr as ValidationAttributeBase;

                    if (attribute == null)
                    {
                        //if attribute is not a validation attribute base
                        //ignore it
                        continue;
                    }

                    ValidationResult result = attribute.isValid(value, prop.Name);

                    if (result != null)
                    {
                        lstResult.Add(result);
                    }
                }

                return(lstResult);
            }
            catch (Exception)
            {
                throw;
            }
        }