z8018
2 天以前 d8dc91f9c1fece5711e38edd1b1274cb9e579015
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace WIDESEA_Core.Helper
{
    public static class ObjectExtension
    {
        /// <summary>
        /// 将字典列表转换为指定类型的对象列表
        /// </summary>
        /// <typeparam name="T">目标对象类型</typeparam>
        /// <param name="dicList">源字典列表</param>
        /// <returns>转换后的对象列表</returns>
        public static List<T> DicToIEnumerable<T>(this List<Dictionary<string, object>> dicList)
        {
            List<T> list = new List<T>();
            foreach (Dictionary<string, object> dic in dicList)
            {
                list.Add(dic.DicToModel<T>());
            }
            return list;
        }
 
        /// <summary>
        /// 将字典转换为指定类型的模型对象
        /// </summary>
        /// <typeparam name="T">目标模型类型</typeparam>
        /// <param name="dic">包含属性值的字典</param>
        /// <returns>转换后的模型对象</returns>
        /// <remarks>
        /// 1. 支持通过属性名、首字母大写或小写的属性名从字典中查找值
        /// 2. 处理了SugarColumn特性的特殊逻辑:主键/自增字段跳过,非空字段校验
        /// 3. 自动进行类型转换
        /// </remarks>
        /// <exception cref="Exception">当非空字段值为null或空时抛出异常</exception>
        public static T DicToModel<T>(this Dictionary<string, object> dic)
        {
            T model = Activator.CreateInstance<T>();
            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<SugarColumn>();
                if (sugarColumn != null)
                {
                    if ((value == null || value.Equals("")))
                    {
                        if (sugarColumn.IsIdentity || sugarColumn.IsPrimaryKey)
                            continue;
                        else if (sugarColumn.IsNullable)
                        {
                            property.SetValue(model, value);
                        }
                        else
                        {
                            throw new Exception($"The value of {property.Name} is null or empty, but it is not allowed to be null or empty.");
                        }
                    }
                    else
                    {
                        property.SetValue(model, value.ChangeType(property.PropertyType));
                    }
 
                    if (sugarColumn.IsIdentity || sugarColumn.IsPrimaryKey)
                        continue;
                }
                if (value != null && sugarColumn != null && !sugarColumn.IsNullable)
                    property.SetValue(model, value.ChangeType(property.PropertyType));
            }
            return model;
        }
    }
}