public static T FillModel<T>(IDataReader dr)
{
using (dr)
{
if (dr.Read())
{
List<string> field = new List<string>(dr.FieldCount);
for (int i = 0; i < dr.FieldCount; i++)
{
field.Add(dr.GetName(i).ToLower());
}
T model = Activator.CreateInstance<T>();
foreach (PropertyInfo property in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
{
if (field.Contains(property.Name.ToLower()) && !SqlBuilder.IsNullOrDBNull(dr[property.Name]))
{
property.SetValue(model, Convert.ChangeType(dr[property.Name], property.PropertyType), null);
}
}
return model;
}
}
return default(T);
}
public static List<T> FillModels<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()) && !SqlBuilder.IsNullOrDBNull(dr[property.Name]))
{
property.SetValue(model, Convert.ChangeType(dr[property.Name], property.PropertyType), null);
}
}
list.Add(model);
}
return list;
}
}