.NET通过字典给类赋值实现代码
废话不多说了,直接贴代码了,具体代码如下所述:
///<summary> /// ///</summary> ///<typeparamname="T"></typeparam> ///<paramname="origin">源数据</param> ///<paramname="target">对象数据</param> ///<paramname="dict">变量名对应字典</param> publicstaticvoidCopyTo<T>(thisobjectorigin,Ttarget,Dictionary<string,string>dict)whereT:class,new() { PropertyInfo[]props=target.GetType().GetProperties(); foreach(PropertyInfoinfoinprops) { varvariable=dict.FirstOrDefault(m=>m.Value==info.Name); if(variable.Key!=null) { stringvariableName=variable.Key; stringchineseName=variable.Value; varpropertyValue= origin.GetType() .GetProperty(variableName) .GetValue(origin,null); if(propertyValue!=null) { if(propertyValue.GetType().IsClass) { } target.GetType() .InvokeMember(chineseName,BindingFlags.SetProperty,null,target, newobject[]{propertyValue}); } } } }
以上所述是小编给大家介绍的.NET通过字典给类赋值实现代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对毛票票网站的支持!