C#中属性PropertyInfo的使用,Dictionary转为Model实例
来源:互联网 发布:php http referer 编辑:程序博客网 时间:2024/05/17 22:44
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);
http://www.51testing.com/html/24/n-933924-3.html
0 0
- C#中属性PropertyInfo的使用,Dictionary转为Model实例
- C#中属性PropertyInfo的使用
- C#中属性PropertyInfo的使用
- 属性PropertyInfo的使用
- C#遍历类的属性 PropertyInfo.Attributes
- C#遍历类的属性 PropertyInfo.Attributes
- C#遍历类的属性 PropertyInfo.Attributes
- C#遍历类的属性 PropertyInfo.Attributes
- C# PropertyInfo的用法
- C#根据字符串得到静态类的属性(PropertyInfo)
- PropertyInfo的使用
- C#中字典的使用Dictionary
- C#中Dictionary的foreach使用KeyValuePair
- C#中字典的使用Dictionary
- C#中Dictionary的用法及用途实例
- C#中Dictionary的用法及用途实例
- C#中Dictionary的用法及用途实例
- C#中Dictionary的用法及用途实例
- iOS9下企业证书打包app的验证过程[图文说明]
- 只用2GB内存在20亿个整数中找到出现次数最多的数
- 在vmware下安装ubuntu
- iOS开发——音频篇——音效的播放
- 指定分词器测试分词结果
- C#中属性PropertyInfo的使用,Dictionary转为Model实例
- 不要轻易在简历上写我热爱编程,我热爱学习—兄弟连IT教育
- 历史格局的认知、节点、路径与命运 --刘仲敬
- 记录我的学习过程
- 常用的字体图标库
- 如何由电磁理论引出牛顿力学下的引力场方程
- 贴一篇我的Javadoc
- 手机usb口通信
- 64位ubuntu 11.10下android开发环境的搭建(jdk+Eclipse+adt+android sdk详细)