优化datarow转换实体类时判断是否包含属性

master
lhiven 6 months ago
parent 6e5d40e19e
commit baefe0daeb

@ -23,13 +23,16 @@ namespace Rs.Framework
PropertyInfo[] pis = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); PropertyInfo[] pis = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (PropertyInfo propertyInfo in pis) foreach (PropertyInfo propertyInfo in pis)
{ {
if(propertyInfo.PropertyType.IsEnum) if(dr.Table.Columns.Contains(propertyInfo.Name))
{ {
propertyInfo.SetValue(entity, Enum.Parse(propertyInfo.PropertyType, dr[propertyInfo.Name].ToString())); if (propertyInfo.PropertyType.IsEnum)
} {
else propertyInfo.SetValue(entity, Enum.Parse(propertyInfo.PropertyType, dr[propertyInfo.Name].ToString()));
{ }
propertyInfo.SetValue(entity, Convert.ChangeType(dr[propertyInfo.Name], propertyInfo.PropertyType)); else
{
propertyInfo.SetValue(entity, Convert.ChangeType(dr[propertyInfo.Name], propertyInfo.PropertyType));
}
} }
} }
return entity; return entity;

Loading…
Cancel
Save