/// <summary> /// 根据attribute验证model /// </summary> 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.NotNullList(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> /// 根据attribute验证model /// </summary> public static List <string> CheckEntity_ <T>(T model) where T : IDBTable { if (model == null) { throw new ArgumentNullException(nameof(model)); } var list = new List <string>(); //checker bool CheckProp(IEnumerable <ValidationAttribute> validators, PropertyInfo p) { var data = p.GetValue(model); foreach (var validator in ConvertHelper.NotNullList(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.HasCustomAttributes_ <NotMappedAttribute>(inherit: false)) { continue; } //忽略父级的验证属性 var validators = prop.GetCustomAttributes_ <ValidationAttribute>(inherit: false); if (!CheckProp(validators, prop)) { continue; } } list = list.NotEmptyAndDistinct(x => x).ToList(); return(list); }
/// <summary> /// 获取map(测试可用) /// </summary> public static void MapEntity <T>(ref T entity, object model, string[] notmap = null) { if (model == null) { throw new Exception("对象为空"); } //读取 var modelproperties = ConvertHelper.NotNullList(model.GetType().GetProperties()); modelproperties = modelproperties.Where(x => x.CanRead).ToList(); //写入 var entityproperties = ConvertHelper.NotNullList(entity.GetType().GetProperties()); entityproperties = entityproperties.Where(x => x.CanWrite).ToList(); if (ValidateHelper.IsPlumpList(notmap)) { entityproperties = entityproperties.Where(x => !notmap.Contains(x.Name)).ToList(); } foreach (var pi in entityproperties) { //属性名和属性类型一样 var modelpi = modelproperties .Where(x => x.Name == pi.Name) .Where(x => x.GetType() == pi.GetType()) .FirstOrDefault(); if (modelpi == null) { continue; } pi.SetValue(entity, modelpi.GetValue(model), null); } }
/// <summary> /// 获取map(测试可用) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="model"></param> public static void MapEntity <T>(ref T entity, object model, string[] notmap = null) { if (model == null) { throw new Exception("对象为空"); } if (notmap == null) { notmap = new string[] { }; } var modelproperties = ConvertHelper.NotNullList(model.GetType().GetProperties()); var entityproperties = ConvertHelper.NotNullList(entity.GetType().GetProperties()); foreach (var pi in entityproperties) { if (notmap.Contains(pi.Name)) { continue; } //属性名和属性类型一样 var modelpi = modelproperties .Where(x => x.Name == pi.Name) .Where(x => x.GetType().Name == pi.GetType().Name) .FirstOrDefault(); if (modelpi == null) { continue; } pi.SetValue(entity, modelpi.GetValue(model), null); } }
/// <summary> /// 包含长度大于0的item,并把他们找出来 /// </summary> /// <param name="list"></param> /// <param name="filtered"></param> /// <returns></returns> public static bool IsPlumpListAfterFilterMeaninglessData(IList <string> list, out List <string> filtered) { filtered = ConvertHelper.NotNullList(list).Where(x => IsPlumpString(x)).ToList(); return(filtered.Count > 0); }
/// <summary> /// 第一个非空字符串 /// </summary> /// <param name="strs"></param> /// <returns></returns> public static string FirstPlumpStrOrNot(params string[] strs) => ConvertHelper.NotNullList(strs).Where(x => ValidateHelper.IsPlumpString(x)).FirstOrDefault();
public static List <T> NotNullEnumerable <T>(IEnumerable <T> list) => ConvertHelper.NotNullList(list);