public static void PopulateObjectList <T>(ValueReader reader, Dictionary <string, string> columnPropertiesMap, List <T> list) { List <T> retVal = list; LinkedList <T> linkList = new LinkedList <T>(); KeyValuePair <string, Method>[] miArray = new KeyValuePair <string, Method> [(columnPropertiesMap.Count - 1) + 1]; Type entType = typeof(T); int mapIndex = 0; foreach (string columnName in columnPropertiesMap.Keys) { string property = columnPropertiesMap[columnName]; MethodInfo mi = entType.GetMethod("set_" + property); if (mi == null) { throw new NotImplementedException(string.Format("Property: {0} is not implemented on Type: {1}", property, entType)); } miArray[mapIndex++] = new KeyValuePair <string, Method>(columnName, new Method() { MI = mi, Type = mi.GetParameters()[0].ParameterType }); } while (reader.Read()) { T entity = Reflection.CreateInstance <T>(); for (int propIndex = 0; propIndex < miArray.Length; propIndex++) { Method mi = miArray[propIndex].Value; object val = reader.GetValue(miArray[propIndex].Key); Type pt = mi.Type; if (val == null || string.IsNullOrEmpty(val.ToString())) { if (mi.Type.IsValueType) { val = Reflection.CreateInstance <object>(mi.Type); } else { val = null; } } if (val != null && mi.Type.IsGenericType && mi.Type.GetGenericTypeDefinition() == typeof(Nullable <>)) { val = Convert.ChangeType(val, mi.Type.GetGenericArguments()[0]); } mi.MI.Invoke(entity, BindingFlags.SetProperty, null, new object[] { val }, CultureInfo.CurrentCulture); } linkList.AddLast(entity); } retVal.Capacity = (retVal.Count + linkList.Count) + 10; foreach (T entity in linkList) { retVal.Add(entity); } linkList.Clear(); if (retVal.Count > 1000) { GC.Collect(2, GCCollectionMode.Forced); } }
public static List <T> PopulateObjectList <T>(ValueReader reader, string columnName) { List <T> retVal = new List <T>(); while (reader.Read()) { retVal.Add((T)reader.GetValue(columnName)); } return(retVal); }
public static T PopulateObject <T>(ValueReader reader, Dictionary <string, string> columnPropertiesMap) { T retVal = default(T); if (reader.Read()) { retVal = Reflection.CreateInstance <T>(); Type retType = typeof(T); foreach (string columnName in columnPropertiesMap.Keys) { string property = columnPropertiesMap[columnName]; MethodInfo mi = retType.GetMethod("set_" + property); if (mi == null) { throw new NotImplementedException(string.Format("Property: {0} is not implemented on Type: {1}", property, retType)); } Type pt = mi.GetParameters()[0].ParameterType; object val = reader.GetValue(columnName); if (val == null || string.IsNullOrEmpty(val.ToString())) { if (pt.IsValueType) { val = Reflection.CreateInstance <object>(pt); } else { val = null; } } if (val != null && pt.IsGenericType && pt.GetGenericTypeDefinition() == typeof(Nullable <>)) { val = Convert.ChangeType(val, pt.GetGenericArguments()[0]); } mi.Invoke(retVal, BindingFlags.SetProperty, null, new object[] { val }, CultureInfo.CurrentCulture); } } return(retVal); }