using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_Core.Attributes; namespace WIDESEAWCS_Core.Utilities { public class ModelValidate { /// <summary> /// 验è¯å®žä½“傿•° /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <returns></returns> public static (bool, string, object?) ValidateModelData<T>(T data) where T : class, new() { Type modelType = typeof(T); if (data == null) return (false, "ä¼ å…¥å‚æ•°ä¸å¯ä¸ºnull", data); ModelValidateAttribute? modelAttribute = modelType.GetCustomAttribute<ModelValidateAttribute>(); if (modelAttribute == null) return (false, $"{modelType.Name}未定义ã€ModelValidateAttribute】特性", data); PropertyInfo[] propertyInfos = modelType.GetProperties(); return SimpleValidate(propertyInfos, data); } public static (bool, string, object?) ValidateModelData<T>(List<T> datas) where T : class, new() { Type modelType = typeof(T); if (datas == null) return (false, "ä¼ å…¥å‚æ•°ä¸å¯ä¸ºnull", datas); if (datas.Count == 0) return (false, "集åˆä¸ªæ•°ä¸å¯ç‰äºŽ0", datas); foreach (T data in datas) { if (data == null) return (false, "ä¼ å…¥å‚æ•°ä¸å¯ä¸ºnull", data); ModelValidateAttribute? modelAttribute = modelType.GetCustomAttribute<ModelValidateAttribute>(); if (modelAttribute == null) return (false, $"{modelType.Name}未定义ã€ModelValidateAttribute】特性", data); PropertyInfo[] propertyInfos = modelType.GetProperties(); (bool, string, object?) result = SimpleValidate(propertyInfos, data); if (!result.Item1) return result; } return (true, $"æˆåŠŸ", datas); } //private static (bool, string, object?) CustomMethodValidate<T>(ModelValidateAttribute modelAttribute, T data) where T : class, new() //{ // try // { // if (modelAttribute.CustomValidateMethodTypeName == null) return (false, $"è‡ªå®šä¹‰éªŒè¯æ–¹æ³•éœ€è¦æä¾›æ–¹æ³•æ‰€åœ¨ç±»çš„ç±»åž‹å¯¹è±¡", data); // if (modelAttribute.CustomValidateMethodName == null) return (false, $"è‡ªå®šä¹‰éªŒè¯æ–¹æ³•éœ€è¦æä¾›æ–¹æ³•å", data); // string path = Path.Combine(AppContext.BaseDirectory, $"{modelAttribute.CustomValidateAssemblyName}.dll"); // Assembly assembly = Assembly.LoadFrom(path); // Type t = assembly.GetType(modelAttribute.CustomValidateAssemblyName + "." + modelAttribute.CustomValidateMethodTypeName); // object bbb = App.GetService(t); // MethodInfo? methodInfo = t.GetMethod(modelAttribute.CustomValidateMethodName); // var result = methodInfo.Invoke(bbb, new object[] { data }); // //MethodInfo? methodInfo = modelAttribute.CustomValidateMethodType.GetMethod(modelAttribute.CustomValidateMethodName); // //if (methodInfo == null) return (false, $"未在该类型对象ã€{modelAttribute.CustomValidateMethodType.Name}ã€‘ä¸æ‰¾åˆ°è¯¥æ–¹æ³•ã€{modelAttribute.CustomValidateMethodName}】", data); // //methodInfo.GetGenericArguments() // return (true, "æˆåŠŸ", data); // } // catch(Exception ex) // { // throw new Exception(); // } //} private static (bool, string, object?) SimpleValidate<T>(PropertyInfo[] propertyInfos, T data) where T : class, new() { try { foreach (PropertyInfo propertyInfo in propertyInfos) { PropertyValidateAttribute? propertyAttribute = propertyInfo.GetCustomAttribute<PropertyValidateAttribute>(); if (propertyAttribute == null) continue; object? value = propertyInfo.GetValue(data, null); if (propertyAttribute.NotNullAndEmpty) { if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}ä¸å¯ä¸ºnull", data); if (string.IsNullOrEmpty(value.ToString())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}ä¸å¯ä¸ºç©ºå—符串", data); } if (propertyAttribute.MinValue > int.MinValue) { if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}ä¸å¯ä¸ºnull", data); if (propertyAttribute.IsContainMinValue) { if (Convert.ToInt32(value.ToString()) < propertyAttribute.MinValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值ã€{value}】ä¸å¯å°äºŽã€{propertyAttribute.MinValue}】", data); } else { if (Convert.ToInt32(value.ToString()) <= propertyAttribute.MinValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值ã€{value}】è¦å¤§äºŽã€{propertyAttribute.MinValue}】", data); } } if (propertyAttribute.MaxValue < int.MaxValue) { if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}ä¸å¯ä¸ºnull", data); if (propertyAttribute.IsContainMaxValue) { if (Convert.ToInt32(value.ToString()) >= propertyAttribute.MaxValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值ã€{value}】ä¸å¯å¤§äºŽã€{propertyAttribute.MaxValue}】", data); } else { if (Convert.ToInt32(value.ToString()) > propertyAttribute.MaxValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值ã€{value}】è¦å°äºŽã€{propertyAttribute.MaxValue}】", data); } } } } catch (Exception ex) { return (false, $"æ•°æ®éªŒè¯å¼‚常,异常信æ¯:{ex.Message}", data); } return (true, "éªŒè¯æˆåŠŸ", data); } } }