Example #1
0
        /// <summary>
        /// Convierte un objeto de tipo System.Data.DataTable a una Lista del tipo <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T">Tipo referencia para serializar.</typeparam>
        /// <param name="dataTable">El contenido a convertir.</param>
        /// <returns>Regresa una nueva Lista del tipo <typeparamref name="T"/> ya con los objetos incorporados.</returns>
        public static List <T> ConvertDataTableToListOfType <T>(System.Data.DataTable dataTable) where T : new()
        {
            List <T> newList = new List <T>();

            PropertyInfo[] properties = typeof(T).GetProperties();
            Dictionary <string, HeaderName> headers = GetCustomAttributesFromPropertiesInClass <HeaderName, T>();

            foreach (DataRow row in dataTable.Rows)
            {
                T newObject = new T();
                foreach (DataColumn column in dataTable.Columns)
                {
                    foreach (PropertyInfo property in properties)
                    {
                        if (property.CanWrite)
                        {
                            if (headers.TryGetValue(property.Name, out HeaderName header))
                            {
                                if (header.Name.Equals(column.ColumnName))
                                {
                                    property.SetValue(newObject, SimpleConverter.ConvertStringToType(row[header.Name].ToString(), property.PropertyType));
                                    break;
                                }
                            }
                            else if (column.ColumnName.Equals(property.Name))
                            {
                                property.SetValue(newObject, SimpleConverter.ConvertStringToType(row[property.Name].ToString(), property.PropertyType));
                                break;
                            }
                        }
                    }
                }

                newList.Add(newObject);
            }

            return(newList);
        }
Example #2
0
        public static Hashtable ConvertDataTableToHashtableOfType <T>(System.Data.DataTable dataTable) where T : Cope <T>, IManageable, new()
        {
            Hashtable newHashTable = new Hashtable();

            if (dataTable != null)
            {
                foreach (DataRow row in dataTable.Rows)
                {
                    PropertyInfo[] properties = typeof(T).GetProperties();
                    T newObject = new T();
                    foreach (PropertyInfo property in properties)
                    {
                        if (dataTable.Columns.Contains(property.Name) && property.CanWrite)
                        {
                            property.SetValue(newObject, SimpleConverter.ConvertStringToType(row[property.Name].ToString(), property.PropertyType));
                        }
                    }
                    newHashTable.Add(Cope <T> .ModelComposition.PrimaryKeyProperty.GetValue(newObject), newObject);
                }
            }

            return(newHashTable);
        }
Example #3
0
        public static Dictionary <dynamic, T> ConvertDataTableToDictionaryOfType <T>(System.Data.DataTable dataTable, string keyName, Type keyType) where T : new()
        {
            Dictionary <dynamic, T> newDictionary = new Dictionary <dynamic, T>();

            if (dataTable != null)
            {
                foreach (DataRow row in dataTable.Rows)
                {
                    T newObject = new T();
                    PropertyInfo[] properties = typeof(T).GetProperties();
                    dynamic        key        = SimpleConverter.ConvertStringToType(row[keyName].ToString(), keyType);

                    foreach (PropertyInfo property in properties)
                    {
                        if (dataTable.Columns.Contains(property.Name) && property.CanWrite)
                        {
                            property.SetValue(newObject, SimpleConverter.ConvertStringToType(row[property.Name].ToString(), property.PropertyType));
                        }
                    }
                    newDictionary.Add(key, newObject);
                }
            }
            return(newDictionary);
        }