/// <summary> /// 验证 /// </summary> /// <param name="target">验证目标</param> public static ValidationResultCollection Validate(object target) { if (target == null) { throw new ArgumentNullException(nameof(target)); } var result = new ValidationResultCollection(); var validationResults = new List <ValidationResult>(); var context = new ValidationContext(target, null, null); var isValid = Validator.TryValidateObject(target, context, validationResults, true); if (!isValid) { result.AddRange(validationResults); } return(result); }
/// <summary> /// 验证 /// </summary> /// <param name="appendAction">追加操作</param> public void Validate(Action <ValidationResultCollection> appendAction = null) { var result = DataAnnotationValidation.Validate(Instance); ResultCollection = new ValidationResultCollection(result); appendAction?.Invoke(ResultCollection); var tempList = ValidateStrategyList.Select(strategy => strategy.Validate(Instance)).ToList(); if (tempList.Any()) { ResultCollection.AddRange(tempList); } if (ResultCollection.IsValid) { return; } if (Handle == null) { Handle = op => op.HandleAll(BingConfig.Current.ValidationHandler);// 如果没有处理器操作,则使用默认操作 } Handle.Invoke(ResultCollection.Handle()); }