Dictionary转为Model实例
生活随笔
收集整理的這篇文章主要介紹了
Dictionary转为Model实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Dictionary<string, object> dic = new Dictionary<string, object>();dic.Add("Id",100);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 > 0 && dic.Count() > 0)
{
for (int i = 0; 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);
?
轉載于:https://www.cnblogs.com/macT/p/10213905.html
總結
以上是生活随笔為你收集整理的Dictionary转为Model实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 函数语法:Js之on和addEventL
- 下一篇: ASP.NET MVC+JQueryEa