public static List <T> FillRow <T>(DataRow r) { List <T> _list = new List <T>(); if (r == null) { return(_list); } try { T objTarget = Activator.CreateInstance <T>(); PropertyInfo[] objProperties = objTarget.GetType().GetProperties(); for (int i = 0; i < objProperties.Length; i++) { PropertyInfo property = objProperties[i]; LazyInitAttribute lazy = new LazyInitAttribute(false); object[] objs = property.GetCustomAttributes(true); if (objs.Length > 0) { lazy = (LazyInitAttribute)objs[0]; } if (lazy.IsLazyInit) { continue; } if (property.CanWrite && r.Table.Columns.Contains(property.Name) && !Convert.IsDBNull(r[property.Name])) { property.SetValue(objTarget, convertType(r[property.Name], property.PropertyType), null); } else if (property.CanWrite && (!r.Table.Columns.Contains(property.Name) || !Convert.IsDBNull(r[property.Name]))) { property.SetValue(objTarget, GetDefaultType(property.PropertyType), null); } } _list.Add(objTarget); } finally { //_dr.Close(); } return(_list); }
public static IList <T> FillCollection <T>(IDataReader _dr) { List <T> _list = new List <T>(); if (_dr == null) { return(_list); } try { while (_dr.Read()) { T objTarget = Activator.CreateInstance <T>(); PropertyInfo[] objProperties = objTarget.GetType().GetProperties(); for (int i = 0; i < objProperties.Length; i++) { PropertyInfo property = objProperties[i]; LazyInitAttribute lazy = new LazyInitAttribute(false); object[] objs = property.GetCustomAttributes(true); if (objs.Length > 0) { lazy = (LazyInitAttribute)objs[0]; } if (lazy.IsLazyInit) { continue; } if (property.CanWrite && !Convert.IsDBNull(_dr[property.Name])) { property.SetValue(objTarget, _dr[property.Name], null); } } _list.Add(objTarget); } } finally { _dr.Close(); } return(_list); }