Dictionary转为Model实例

Dictionary<string, object> dic = new Dictionary<string, object>();
  dic.Add("Id",);
  dic.Add("Name", "keso");
  dic.Add("Group", "程序员");
  转换字典方法:
public static T ConvertDic<T>(Dictionary<string, object> dic)
{
T model = Activator.CreateInstance<T>();
PropertyInfo[] modelPro = model.GetType().GetProperties();
if (modelPro.Length > && dic.Count() > )
{
for (int i = ; i < modelPro.Length; i++)
{
if (dic.ContainsKey(modelPro[i].Name))
{
modelPro[i].SetValue(model, dic[modelPro[i].Name], null);
}
}
}
return model;
}
  最后的调用:
  User user = ConvertDic<User>(dic);

第二种

Pedestrian_IllegalEntity  T =JsonConvert.DeserializeObject<Pedestrian_IllegalEntity>(JsonConvert.SerializeObject(dic));
上一篇:dubbo的简单实现


下一篇:webpack文件笔记