using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace WIDESEAWCS_Core.Helper { public static class ObjectExtension { public static List DicToIEnumerable(this List> dicList) { List list = new List(); foreach (Dictionary dic in dicList) { list.Add(dic.DicToModel()); } return list; } public static T DicToModel(this Dictionary dic) { T model = Activator.CreateInstance(); PropertyInfo[] propertyInfos = typeof(T).GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance); foreach (var property in propertyInfos) { object? value = null; if (!dic.TryGetValue(property.Name, out value)) { if (!dic.TryGetValue(property.Name.FirstLetterToUpper(), out value)) { if (!dic.TryGetValue(property.Name.FirstLetterToLower(), out value)) { continue; } } }; SugarColumn? sugarColumn = property.GetCustomAttribute(); if (sugarColumn != null && sugarColumn.IsIdentity && (value == null || value.Equals(""))) { continue; } if (value != null && sugarColumn != null && !sugarColumn.IsNullable) property.SetValue(model, value.ChangeType(property.PropertyType)); } return model; } } }