反射model動態快速賦值

XMLopr xmlpro = new XMLopr();
string xmlpath = WebConfigurationManager.AppSettings["fileinfo_Filexmlpath"].ToString();
xmlpath = Server.MapPath(xmlpath);
Dictionary<string, string> filedmap = xmlpro.ReadXmlFiled(xmlpath);
if (filedmap == null)
{
result="讀取字段匹配設置文檔失敗!";
return result;
}
int i = 0;
foreach (KeyValuePair<string,string> 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);
}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章