using HaBookCms.ContextConfig.Exceptions; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel.DataAnnotations; using System.Text; namespace HaBookCms.Common.ValidateHelper { public static class ValidateHelper { public static string AllowCountry { get; set; } /// /// 验证对象是否有效 /// /// 要验证的对象 /// /// public static bool IsValid(this object obj, Collection validationResults) { return Validator.TryValidateObject(obj, new ValidationContext(obj, null, null), validationResults, true); } /// /// 验证对象是否有效 /// /// 要验证的对象 /// public static bool IsValid(object value) { var results = new Collection(); var validationContext = new ValidationContext(value, null, null); bool f= Validator.TryValidateObject(value, validationContext,results ,true); if (!f) { string s = ""; foreach (ValidationResult result in results) { IEnumerator enumerator= result.MemberNames.GetEnumerator(); enumerator.MoveNext(); s = s + enumerator.Current + ","; } throw new BizException(s+"字段不正确"); } return f; } } }