Tiandele
2026-03-20 daea1a90c2fa1b5cc2f52e62be15bd95cc4155f6
ÏîÄ¿´úÂë/WMS/WMSServer/WIDESEA_Core/Helper/ObjectExtension.cs
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Primitives;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -23,26 +24,56 @@
        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)
            #region
            //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;
            //            }
            //        }
            //    }
            //    // æ£€æŸ¥æ˜¯å¦ä¸ºç©ºå­—符串
            //    if (!string.IsNullOrEmpty(value?.ToString()))
            //    {
            //        // å°†å€¼è½¬æ¢ä¸ºå±žæ€§çš„类型并设置属性值
            //        property.SetValue(model, value.ChangeType(property.PropertyType));
            //    }
            //}
            //return model;
            #endregion
            T? model = Activator.CreateInstance<T>();
            try
            {
                object value = null;
                if (!dic.TryGetValue(property.Name, out value))
                model = JsonConvert.DeserializeObject<T>(dic.Serialize());
            }
            catch { }
            if (model == null)
            {
                PropertyInfo[] propertyInfos = typeof(T).GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance);
                foreach (var property in propertyInfos)
                {
                    if (!dic.TryGetValue(property.Name.FirstLetterToUpper(), out value))
                    object value = null;
                    if (!dic.TryGetValue(property.Name, out value))
                    {
                        if (!dic.TryGetValue(property.Name.FirstLetterToLower(), out value))
                        if (!dic.TryGetValue(property.Name.FirstLetterToUpper(), out value))
                        {
                            continue;
                            if (!dic.TryGetValue(property.Name.FirstLetterToLower(), out value))
                            {
                                continue;
                            }
                        }
                    }
                }
                // æ£€æŸ¥æ˜¯å¦ä¸ºç©ºå­—符串
                if (!string.IsNullOrEmpty(value?.ToString()))
                {
                    // å°†å€¼è½¬æ¢ä¸ºå±žæ€§çš„类型并设置属性值
                    property.SetValue(model, value.ChangeType(property.PropertyType));
                    ;
                    property.SetValue(model, value?.ToString().ChangeType(property.PropertyType));
                }
            }
            return model;