您现在的位置是:网站首页> 编程资料编程资料
.NET通过字典给类赋值实现代码_实用技巧_
2023-05-24
221人已围观
简介 .NET通过字典给类赋值实现代码_实用技巧_
废话不多说了,直接贴代码了,具体代码如下所述:
////// /// ////// 源数据 /// 对象数据 /// 变量名对应字典 public static void CopyTo (this object origin, T target,Dictionary dict)where T :class,new() { PropertyInfo[] props = target.GetType().GetProperties(); foreach (PropertyInfo info in props) { var variable = dict.FirstOrDefault(m => m.Value == info.Name); if (variable.Key!=null) { string variableName = variable.Key; string chineseName = variable.Value; var propertyValue = origin.GetType() .GetProperty(variableName) .GetValue(origin, null); if (propertyValue != null) { if (propertyValue.GetType().IsClass) { } target.GetType() .InvokeMember(chineseName, BindingFlags.SetProperty, null, target, new object[] { propertyValue }); } } } }
以上所述是小编给大家介绍的.NET通过字典给类赋值实现代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对网站的支持!
您可能感兴趣的文章:
相关内容
- .NET添加时间戳防止重放攻击_实用技巧_
- .NET下通过HttpListener实现简单的Http服务_实用技巧_
- C#开发微信 二维码鼠标滑动 图像显示隐藏效果(推荐)_实用技巧_
- asp.net uploadify实现多附件上传功能_实用技巧_
- ASP.NET MVC 微信JS-SDK认证_实用技巧_
- .Net 垃圾回收机制详细介绍_实用技巧_
- ASP.NET WebAPi(selfhost)实现文件同步或异步上传_实用技巧_
- .Net core下直接执行SQL语句并生成DataTable的实现方法_实用技巧_
- asp.net core集成kindeditor实现图片上传功能_实用技巧_
- ASP.NET文件上传Upload的实现方法_实用技巧_
