public static List <T> CreateInstanceList <T>(DataTable dt, ref string strError, BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance) { DateTime dtStart = DateTime.Now; // Validation if (typeof(T).HasParameterlessConstructor() == false) { return(null); } // Set Cache Repository DataTable CacheRepository.DataTable = dt; // Spider Crawl Load Reflection And Data ClassTypeInformation classInfo = CacheRepository.LoadTypeInformation(typeof(T), bindingFlags); // Validation if (classInfo == null) { return(default(List <T>)); } // Load All Class Type DataTables CacheRepository.ClassTypeList.ForEach(cls => cls.LoadDataTable()); // Create Instance List List <object> listTemp = classInfo.CreateInstanceList(classInfo, classInfo.DataTable, ref strError, bindingFlags); // Validation if (listTemp == null) { return(default(List <T>)); } // Convert Objects To Type <T> List <T> listObjects = (List <T>)listTemp.Select(obj => (T)obj).ToList(); // Remove Null Objects listObjects = listObjects.Where(obj => obj != null).ToList(); return(listObjects); }
private void LoadCollectionList(object obj, DataRow dr, List <MemberTypeInformation> listPrimaryKeyMembers, BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance) { string strError = ""; // Loop Collection Fields foreach (FieldInfo fieldChildCollection in obj.CollectionFields(bindingFlags)) { #region Get Class Information // Get Cached Class Information Collection ClassTypeInformation classInfoCollection = CacheRepository.LoadTypeInformation(fieldChildCollection.CollectionFieldType(), bindingFlags); // Validation if (classInfoCollection == null || classInfoCollection.DataTable == null || classInfoCollection.CanBeHydrated == false) { return; } #endregion #region Filter Class DataTable // Create Data Filter string strFilter = String.Join(" AND ", listPrimaryKeyMembers.Select(pk => pk.GetQueryFilterString(dr[pk.ColumnName])).ToArray()); // Get DataRows Based On Filter DataRow[] rows = classInfoCollection.DataTable.Select(strFilter); // Validation if (rows == null || rows.Count() == 0) { return; } // Copy Rows To DataTable DataTable dtInstance = rows.CopyToDataTable(); #endregion #region Create Instance List With Filtered DataTable // Create Instance List List <object> listItems = classInfoCollection.CreateInstanceList(classInfoCollection, dtInstance, ref strError, bindingFlags); // Validation if (listItems == null) { continue; } // Create List Instance object objList = Activator.CreateInstance(fieldChildCollection.FieldType, null); // Loop Instance List foreach (object objItem in listItems) { // Invoke Add Method And Add Instance To List fieldChildCollection.FieldType.GetMethod("Add") .Invoke(objList, new object[] { objItem }); } // Set List Value fieldChildCollection.SetValue(obj, objList); #endregion } }