Example #1
0
        internal static ClassTypeInformation LoadTypeInformation(Type type, BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance)
        {
            // Validation
            if (ClassTypeList == null)
            {
                return(null);
            }

            // Check To See If Type Is Already Cached
            ClassTypeInformation classTypeInfo = ClassTypeList.Where(cls => cls.Type == type).FirstOrDefault();

            // Validation
            if (classTypeInfo != null)
            {
                return(classTypeInfo);
            }

            // Create Class Information
            ClassTypeInformation classInfo = new ClassTypeInformation(type);

            // Add Class Information To Cache List
            ClassTypeList.Add(classInfo);

            // For Each Class Child Type
            classInfo.ChildTypes
            .ForEach(childType =>
                     // Load Child Type Information
                     LoadTypeInformation(childType, bindingFlags));

            return(classInfo);
        }
Example #2
0
        internal void LoadChildMemberData(object obj, DataRow dr, BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance)
        {
            try
            {
                #region Get Class Information

                // Get Cached Class Information Collection
                ClassTypeInformation classInfo = CacheRepository.LoadTypeInformation(obj.GetType(), bindingFlags);

                // Validation
                if (classInfo == null || classInfo.DataTable == null)
                {
                    return;
                }

                #endregion

                #region Get Class Common Primary Keys

                // Create New List Of Common Primary Keys
                List <MemberTypeInformation> listCommonPrimaryKeys = new List <MemberTypeInformation>();

                // Loop Primary Key Members
                foreach (MemberTypeInformation memberPK in this.PrimaryKeyMembers)
                {
                    MemberTypeInformation commonPrimaryKey = classInfo.PrimaryKeyMembers.Where(pk => pk.ColumnName == memberPK.ColumnName).FirstOrDefault();

                    // Validation
                    if (commonPrimaryKey != null)
                    {
                        // Add Primary Key To List
                        listCommonPrimaryKeys.Add(commonPrimaryKey);
                    }
                }

                #endregion

                #region Load Collection And Instance Members

                // Load Object Instance Properties
                this.LoadInstanceList(obj, listCommonPrimaryKeys, bindingFlags);

                // Load Instance Collection Properties
                this.LoadCollectionList(obj, dr, listCommonPrimaryKeys, bindingFlags);

                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR IN TYPE " + this.Type + " - ROW INDEX: " + this.DataTable.Rows.IndexOf(dr) + " Detail: " + ex.ToString());
            }
        }
Example #3
0
        private void LoadInstanceList(object obj, List <MemberTypeInformation> listPrimaryKeyMembers, BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance)
        {
            string strError = "";

            // Loop Instance Fields
            foreach (FieldInfo fieldChildInstance in obj.InstanceFields(bindingFlags))
            {
                #region Get Class Information

                // Get Cached Class Information Collection
                ClassTypeInformation classInfoInstance = CacheRepository.LoadTypeInformation(fieldChildInstance.CollectionFieldType(), bindingFlags);

                // Validation
                if (classInfoInstance == null || classInfoInstance.DataTable == null || classInfoInstance.CanBeHydrated == false)
                {
                    return;
                }

                #endregion

                #region Filter Class DataTable

                // Create Data Filter
                string strFilter = String.Join(" AND ", listPrimaryKeyMembers.Select(pk => pk.ColumnName + "=" + classInfoInstance.DataTable.Rows[0][pk.ColumnName]).ToArray());

                // Get DataRow
                DataRow drInstance = classInfoInstance.DataTable.Select(strFilter).First();

                // Validation
                if (drInstance == null)
                {
                    return;
                }

                #endregion

                #region Create Instance With Filtered DataTable

                // Create Instance
                object objNew = classInfoInstance.CreateInstance(fieldChildInstance.FieldType, drInstance, ref strError, bindingFlags);

                // Set Value
                fieldChildInstance.SetValue(obj, objNew);

                #endregion
            }
        }
Example #4
0
        internal List <object> CreateInstanceList(ClassTypeInformation classInfo, DataTable dt, ref string strError, BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance)
        {
            List <object> listObjects = new List <object>();

            try
            {
                // Validation
                if (classInfo.Type.HasParameterlessConstructor() == false)
                {
                    return(null);
                }

                // Loop DataRows
                foreach (DataRow dr in dt.Rows)
                {
                    // Check Kill Switch To Break Out Of Recursive Reflection
                    if (CacheRepository.KILL_SWITCH == true)
                    {
                        break;
                    }

                    // Create New Instance
                    object obj = this.CreateInstance(classInfo.Type, dr, ref strError, bindingFlags);

                    // Validation
                    if (obj == null || strError != "")
                    {
                        return(null);
                    }

                    // Load Child Member Data
                    this.LoadChildMemberData(obj, dr, bindingFlags);

                    // Add Object To List
                    listObjects.Add(obj);
                }

                return(listObjects);
            }
            catch (Exception ex)
            {
                strError = ex.ToString();

                return(null);
            }
        }
Example #5
0
        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);
        }
Example #6
0
        public static T CreateInstance <T>(DataRow dr, ref string strError, BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance)
        {
            try
            {
                // Get Cached Class Information Instance
                ClassTypeInformation classInfo = CacheRepository.ClassTypeList.Where(cls => cls.Type == typeof(T)).FirstOrDefault();

                // Validation
                if (classInfo == null)
                {
                    return(default(T));
                }

                object obj = classInfo.CreateInstance(typeof(T), dr, ref strError, bindingFlags);

                return((T)obj);
            }
            catch (Exception ex)
            {
                strError = ex.ToString();

                return(default(T));
            }
        }
Example #7
0
        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
            }
        }
Example #8
0
        public static DataTable CreateDataTable <T>(List <T> listObjects, ref string strError)
        {
            // Get Class Type
            ClassTypeInformation classInfo = CacheRepository.ClassTypeList.Where(cls => cls.Type == typeof(T)).FirstOrDefault();

            // Validation
            if (classInfo == null)
            {
                return(null);
            }

            // Create New DataTable
            DataTable dt = new DataTable();

            // Loop Class Type Members
            foreach (MemberTypeInformation member in classInfo.Members)
            {
                // Validation
                if (member.ColumnName == null || member.ColumnName == "" || member.DataType == null || member.FieldInfo == null)
                {
                    continue;
                }

                DataColumn column = new DataColumn(member.ColumnName, member.DataType);
                column.AllowDBNull = member.Nullable;
                if (member.DataType == typeof(string))
                {
                    column.MaxLength = (member.FieldLength > 0) ? member.FieldLength : 255;
                }

                dt.Columns.Add(column);
            }

            // Loop Object List
            foreach (T obj in listObjects)
            {
                // Create New Row
                DataRow dr = dt.NewRow();

                // Loop Class Members
                foreach (MemberTypeInformation member in classInfo.Members)
                {
                    // Validation
                    if (member.ColumnName == null || member.ColumnName == "" || member.DataType == null || member.FieldInfo == null)
                    {
                        continue;
                    }

                    // Get Member Value
                    object objValue = member.FieldInfo.GetValue(obj);

                    // Set DataColumn Value
                    dr[member.ColumnName] = objValue;
                }

                // Add Row To Table
                dt.Rows.Add(dr);
            }

            return(dt);
        }