反射model动态快速赋值

XMLopr xmlpro = new XMLopr();
string xmlpath = WebConfigurationManager.AppSettings["fileinfo_Filexmlpath"].ToString();
xmlpath = Server.MapPath(xmlpath);
Dictionary filedmap = xmlpro.ReadXmlFiled(xmlpath);
if (filedmap == null)
{
result="读取字段匹配设置文档失败!";
return result;
}
int i = 0;
foreach (KeyValuePair keypair in filedmap)
{
//获取值
string value =dr[keypair.Key.Trim()]==null?"":dr[keypair.Key.Trim()].ToString().Trim();
//获取映射字段
string mapfiled = keypair.Value.Trim();
//判断映射字段类型
PropertyInfo property = fileModel.GetType().GetProperty(mapfiled);
if (property.PropertyType.Name.ToLower().Equals("string"))
{
property.SetValue(fileModel,value,null);
}
else if (property.PropertyType.Name.Equals("int32"))
{
property.SetValue(fileModel, int.Parse(value), null);
}
else if (property.PropertyType.Name.Equals("DateTime"))
{
if(value.Trim()!=string.Empty)
property.SetValue(fileModel,DateTime.Parse(value), null);
}
}

 

创新互联公司主营云州网站建设的网络公司,主营网站建设方案,app开发定制,云州h5微信小程序定制开发搭建,云州网站营销推广欢迎云州等地区企业咨询


分享名称:反射model动态快速赋值
标题网址:http://csdahua.cn/article/gsdode.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流