迈克鸿飞,漯河市政府网,许文哲
废话不多说了,直接贴代码了,具体代码如下所述:
/// <summary> /// /// </summary> /// <typeparam name="t"></typeparam> /// <param name="origin">源数据</param> /// <param name="target">对象数据</param> /// <param name="dict">变量名对应字典</param> public static void copyto<t>(this object origin, t target,dictionary<string,string> 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 Core Web Api项目与在NginX下发布的方法
asp.net core3.1 引用的元包dll版本兼容性问题解决方案
IdentityServer4实现.Net Core API接口权限认证(快速入门)
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
网友评论