/// <summary> /// 根据attribute验证model /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <returns></returns> public static List <string> CheckEntity_ <T>(T model) where T : IDBTable { var list = new List <string>(); if (model == null) { list.Add("实体对象不能为Null"); return(list); } //checker bool CheckProp(IEnumerable <ValidationAttribute> validators, object data, PropertyInfo p) { foreach (var validator in ConvertHelper.NotNullEnumerable(validators)) { if (!validator.IsValid(data)) { var msg = ConvertHelper.GetString(validator.ErrorMessage).Trim(); if (!ValidateHelper.IsPlumpString(msg)) { msg = $"字段{p.Name}未通过{validator.GetType().Name}标签的验证"; } list.Add(msg); return(false); } } return(true); }; foreach (var prop in model.GetType().GetProperties()) { if (prop.GetCustomAttributes <NotMappedAttribute>().Any()) { continue; } var value = prop.GetValue(model); if (!CheckProp(prop.GetCustomAttributes_ <ValidationAttribute>(), value, prop)) { continue; } } list = list.Where(x => IsPlumpString(x)).Distinct().ToList(); if (ValidateHelper.IsPlumpList(list)) { Console.WriteLine("".Join_(list)); } return(list); }
/// <summary> /// 第一个非空字符串 /// </summary> /// <param name="strs"></param> /// <returns></returns> public static string FirstPlumpStrOrNot(params string[] strs) => ConvertHelper.NotNullEnumerable(strs).Where(x => ValidateHelper.IsPlumpString(x)).FirstOrDefault();