public static IEnumerable<T> Parse<T>(IEnumerable<DataRow> rows) where T : class, new()
{
if (rows == null || Enumerable.FirstOrDefault<DataRow>(rows) == null)
return (IEnumerable<T>) new T[0];
PropertyInfo[] properties = typeof (T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty);
List<T> list = new List<T>();
foreach (DataRow row in rows)
{
T instance = Activator.CreateInstance<T>();
DbHelper.Parse((object) instance, (IEnumerable<PropertyInfo>) properties, row);
list.Add(instance);
}
return (IEnumerable<T>) list;
}
private static void Parse(object obj, IEnumerable<PropertyInfo> properties, DataRow row)
{
foreach (PropertyInfo propertyInfo in properties)
{
if (DataRowExtension.HasValue(row, propertyInfo.Name))
{
try
{
propertyInfo.SetValue(obj, DbHelper.ConvertType(CultureInfo.CurrentCulture, row[propertyInfo.Name], propertyInfo.PropertyType), (object[]) null);
}
}
catch{ }
}
}
时间: 2024-10-21 19:18:51