c#——字典转化为对象(dictionary to object)
来源:互联网 发布:华硕笔记本win10优化 编辑:程序博客网 时间:2024/06/05 00:35
/// <summary> /// 字典类型转化为对象 /// </summary> /// <param name="dic"></param> /// <returns></returns> public T DicToObject<T>(Dictionary<string, object> dic) where T : new() { var md = new T(); CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture; TextInfo textInfo = cultureInfo.TextInfo; foreach (var d in dic) { var filed = textInfo.ToTitleCase(d.Key); try { var value = d.Value; md.GetType().GetProperty(filed).SetValue(md, value); } catch (Exception e) { } } return md; }
其中将字符串转化为首字母大写,既符合c#属性命名规范
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
var filed = textInfo.ToTitleCase(“name”);
得到的field值为:Name
其中对象的赋值利用了反射原理,获得对象的属性,并为它们一一赋值。
md.GetType().GetProperty(filed).SetValue(md, value);
0 0
- c#——字典转化为对象(dictionary to object)
- Object C Dictionary字典
- object-c 反射 将class转化为dictionary
- JObject——转化为字典IDictionary<string, object>
- 把字典转化为对象
- iOS 对象转化为字典
- Dictionary对象(字典对象)
- iOS对象(字典或数组)转化为JSon字符串
- iOS对象(字典或数组)转化为JSon字符串
- iOS对象(字典或数组)转化为JSon…
- Json转化为Java对象(object,List等)
- 浅谈DICTIONARY(字典)对象
- Scripting.Dictionary字典对象
- Object-C基础(13)——字典与谓词
- objective-c dictionary(字典)
- objective-c dictionary(字典)
- objective-c dictionary(字典)
- c#,字典,Dictionary
- Java集合框架的理解
- android 启动页面
- SQL Server 2005 常用数据类型详解
- 网站架构-参考文章
- iOS-通俗易懂的微信支付接入和爬坑指南,十分钟轻松搞完
- c#——字典转化为对象(dictionary to object)
- 深入理解Java的接口和抽象类
- java集合框架
- 根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串
- 一次混杂程序跑在一台机器上的内存排查 (1)
- JsonDB的一个使用实例
- 这里一定要取别名
- Android6.0运行时权限处理(项目中使用到的一种形式,没有使用第三方库)
- 操作系统--进程通信