huangxiaoqiang
2025-06-12 3d14b1056267a25dc8aa0ee72f117daa84045f5c
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/Helper/ObjectExtension.cs
@@ -21,21 +21,42 @@
        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);
            //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;
            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))
                if (!dic.TryGetValue(property.Name, out value))
                {
                    continue;
                    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;
        }
    }
}