Esempio n. 1
0
        /// <summary>
        /// 验证指定的实体
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>该实体的验证结果</returns>
        public override ValidationResult Validate(object entity)
        {
            var name             = _customerValidatorType.FullName;
            var customerInstance = _customerValidatorType.Assembly.CreateInstance
                                       (_customerValidatorType.FullName);
            dynamic          ci  = customerInstance;
            dynamic          ent = entity;
            var              f   = ci.Validate(ent);
            ValidationResult ret = new ValidationResult();

            ret.EntityName = entity.GetType().Name;
            ret.Errors     = new List <ValidationFailure>();
            if (f.Errors != null && f.Errors.Count > 0)
            {
                foreach (var e in f.Errors)
                {
                    ValidationFailure mee = new ValidationFailure();
                    mee.AttemptedValue = e.AttemptedValue;
                    mee.CustomState    = e.CustomState;
                    mee.ErrorMessage   = e.ErrorMessage;
                    mee.PropertyName   = e.PropertyName;
                    ret.Errors.Add(mee);
                }
            }
            return(ret);
        }
Esempio n. 2
0
        public static ValidationResult OneErrorResult(string entityName, string propertyName, string error)
        {
            var re = new ValidationResult();

            re.EntityName = entityName;
            ValidationFailure f = new ValidationFailure();

            f.PropertyName = propertyName;
            f.ErrorMessage = error;
            re.Errors.Add(f);
            return(re);
        }