在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
/// <summary> /// 把Model转换为DataRow /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mod"></param> /// <returns></returns> public static T ParseDictionaryToModel<T>(Dictionary<string, string> dict) { // T obj = default(T); obj = Activator.CreateInstance<T>(); //根据Key值设定 Columns foreach (KeyValuePair<string, string> item in dict) { PropertyInfo prop = obj.GetType().GetProperty(item.Key); if(!string.IsNullOrEmpty(item.Value)) { object value = item.Value; //Nullable 获取Model类字段的真实类型 Type itemType = Nullable.GetUnderlyingType(prop.PropertyType) == null ? prop.PropertyType : Nullable.GetUnderlyingType(prop.PropertyType); //根据Model类字段的真实类型进行转换 prop.SetValue(obj, Convert.ChangeType(value, itemType), null); } } return obj; }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论