1
huangxiaoqiang
2026-03-17 7a663544bda26aaabcad039c726b87d0c65be5f0
1
已修改2个文件
85 ■■■■ 文件已修改
项目代码/WCS/WCSServer/WIDESEAWCS_Core/Helper/ObjectExtension.cs 42 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
项目代码/WMS/WMSServer/WIDESEA_Core/Helper/ObjectExtension.cs 43 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ÏîÄ¿´úÂë/WCS/WCSServer/WIDESEAWCS_Core/Helper/ObjectExtension.cs
@@ -1,4 +1,5 @@
using System;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
@@ -21,20 +22,49 @@
        public static T DicToModel<T>(this Dictionary<string, object> dic)
        {
            T model = Activator.CreateInstance<T>();
            #region
            //T model = Activator.CreateInstance<T>();
            //PropertyInfo[] propertyInfos = typeof(T).GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance);
            //Dictionary<string, object> upperDic = dic.ToDictionary(k => k.Key.ToUpper(), v => v.Value);
            //foreach (var property in propertyInfos)
            //{
            //    object value = null;
            //    if (!upperDic.TryGetValue(property.Name.ToUpper(), out value))
            //    {
            //        continue;
            //    }
            //    property.SetValue(model, value?.ToString().ChangeType(property.PropertyType));
            //}
            //return model;
            #endregion
            T? model = Activator.CreateInstance<T>();
            try
            {
                model = JsonConvert.DeserializeObject<T>(dic.Serialize());
            }
            catch{ }
            if (model == null)
            {
            PropertyInfo[] propertyInfos = typeof(T).GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance);
            Dictionary<string, object> upperDic = dic.ToDictionary(k => k.Key.ToUpper(), v => v.Value);
            foreach (var property in propertyInfos)
            {
                object value = null;
                if (!upperDic.TryGetValue(property.Name.ToUpper(), out value))
                    if (!dic.TryGetValue(property.Name, out value))
                    {
                        if (!dic.TryGetValue(property.Name.FirstLetterToUpper(), out value))
                        {
                            if (!dic.TryGetValue(property.Name.FirstLetterToLower(), out value))
                {
                    continue;
                }
                        }
                    };
                property.SetValue(model, value?.ToString().ChangeType(property.PropertyType));
            }
            }
            return model;
        }
    }
ÏîÄ¿´úÂë/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,7 +24,40 @@
        public static T DicToModel<T>(this Dictionary<string, object> dic)
        {
            T model = Activator.CreateInstance<T>();
            #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
            {
                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)
            {
@@ -38,11 +72,8 @@
                        }
                    }
                }
                // æ£€æŸ¥æ˜¯å¦ä¸ºç©ºå­—符串
                if (!string.IsNullOrEmpty(value?.ToString()))
                {
                    // å°†å€¼è½¬æ¢ä¸ºå±žæ€§çš„类型并设置属性值
                    property.SetValue(model, value.ChangeType(property.PropertyType));
                    ;
                    property.SetValue(model, value?.ToString().ChangeType(property.PropertyType));
                }
            }
            return model;