利用反射实现通用的DataReader转List、DataReader转实体类
dataReader转list和转model有时候经常用,为了偷懒嘛,少写代码倒是不少,用缓存基本上也可以把性能补过来吧,反正我没有测试过,哪位同仁测试过,大家可以研究研究~~
把整理好的代码贴出来如下:大家可以参考参考 有点乱,注释就没有写了,地球人应该都看得懂,呵呵
public static T ReaderToModel<T>(IDataReader dr)
{
try
{
using (dr)
{
if (dr.Read())
{
List<string> list = new List<string>(dr.FieldCount);
for (int i = 0; i < dr.FieldCount; i++)
{
list.Add(dr.GetName(i).ToLower());
}
T model = Activator.CreateInstance<T>();
foreach (PropertyInfo pi in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
{
if (list.Contains(pi.Name.ToLower()))
{
if (!IsNullOrDBNull(dr[pi.Name]))
{
pi.SetValue(model, HackType(dr[pi.Name], pi.PropertyType), null);
}
}
}
return model;
}
}
return default(T);
}
catch (Exception ex)
{
throw ex;
}
}
public static List<T> ReaderToList<T>(IDataReader dr)
{
using (dr)
{
List<string> field = new List<string>(dr.FieldCount);
for (int i = 0; i < dr.FieldCount; i++)
{
field.Add(dr.GetName(i).ToLower());
}
List<T> list = new List<T>();
while (dr.Read())
{
T model = Activator.CreateInstance<T>();
foreach (PropertyInfo property in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
{
if (field.Contains(property.Name.ToLower()))
{
if (!IsNullOrDBNull(dr[property.Name]))
{
property.SetValue(model, HackType(dr[property.Name], property.PropertyType), null);
}
}
}
list.Add(model);
}
return list;
}
}
//这个类对可空类型进行判断转换,要不然会报错
private static object HackType(object value, Type conversionType)
{
if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value == null)
return null;
System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(conversionType);
conversionType = nullableConverter.UnderlyingType;
}
return Convert.ChangeType(value, conversionType);
}
private static bool IsNullOrDBNull(object obj)
{
return ((obj is DBNull) || string.IsNullOrEmpty(obj.ToString())) ? true : false;
}
{
try
{
using (dr)
{
if (dr.Read())
{
List<string> list = new List<string>(dr.FieldCount);
for (int i = 0; i < dr.FieldCount; i++)
{
list.Add(dr.GetName(i).ToLower());
}
T model = Activator.CreateInstance<T>();
foreach (PropertyInfo pi in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
{
if (list.Contains(pi.Name.ToLower()))
{
if (!IsNullOrDBNull(dr[pi.Name]))
{
pi.SetValue(model, HackType(dr[pi.Name], pi.PropertyType), null);
}
}
}
return model;
}
}
return default(T);
}
catch (Exception ex)
{
throw ex;
}
}
public static List<T> ReaderToList<T>(IDataReader dr)
{
using (dr)
{
List<string> field = new List<string>(dr.FieldCount);
for (int i = 0; i < dr.FieldCount; i++)
{
field.Add(dr.GetName(i).ToLower());
}
List<T> list = new List<T>();
while (dr.Read())
{
T model = Activator.CreateInstance<T>();
foreach (PropertyInfo property in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
{
if (field.Contains(property.Name.ToLower()))
{
if (!IsNullOrDBNull(dr[property.Name]))
{
property.SetValue(model, HackType(dr[property.Name], property.PropertyType), null);
}
}
}
list.Add(model);
}
return list;
}
}
//这个类对可空类型进行判断转换,要不然会报错
private static object HackType(object value, Type conversionType)
{
if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value == null)
return null;
System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(conversionType);
conversionType = nullableConverter.UnderlyingType;
}
return Convert.ChangeType(value, conversionType);
}
private static bool IsNullOrDBNull(object obj)
{
return ((obj is DBNull) || string.IsNullOrEmpty(obj.ToString())) ? true : false;
}
每天进步一点点...