| | |
| | | using Microsoft.Extensions.Primitives; |
| | | using Microsoft.IdentityModel.Tokens; |
| | | using Newtonsoft.Json; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | |
| | | |
| | | 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) |
| | | { |
| | |
| | | } |
| | | } |
| | | } |
| | | // æ£æ¥æ¯å¦ä¸ºç©ºå符串 |
| | | if (!string.IsNullOrEmpty(value?.ToString())) |
| | | { |
| | | // å°å¼è½¬æ¢ä¸ºå±æ§çç±»åå¹¶è®¾ç½®å±æ§å¼ |
| | | property.SetValue(model, value.ChangeType(property.PropertyType)); |
| | | ; |
| | | property.SetValue(model, value?.ToString().ChangeType(property.PropertyType)); |
| | | } |
| | | } |
| | | return model; |