コード例 #1
0
        /// <summary>
        /// Convierte la primer fila DataRow dentro del System.Data.DataTable a un Objeto del tipo <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T">Tipo referencia para serializar.</typeparam>
        /// <param name="dataTable">El contenido a convertir.</param>
        /// <returns>Regresa un objeto ya convertido al tipo <typeparamref name="T"/>.</returns>
        public static T ConvertDataTableToObjectOfType <T>(System.Data.DataTable dataTable) where T : new()
        {
            T newObject = new T();

            if (dataTable.Rows.Count > 0)
            {
                PropertyInfo[] properties = typeof(T).GetProperties();
                Dictionary <string, HeaderName> headers = GetCustomAttributesFromPropertiesInClass <HeaderName, 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(dataTable.Rows[0][header.Name].ToString(), property.PropertyType));
                                    break;
                                }
                            }
                            else if (column.ColumnName.Equals(property.Name))
                            {
                                property.SetValue(newObject, SimpleConverter.ConvertStringToType(dataTable.Rows[0][property.Name].ToString(), property.PropertyType));
                                break;
                            }
                        }
                    }
                }
            }

            return(newObject);
        }
コード例 #2
0
ファイル: FileSerializer.cs プロジェクト: jorgemk85/OneData
        private static void SetValueInProperty <T>(Dictionary <string, string> columns, PropertyInfo property, HeaderName headerNameAttribute, string header, T newObj)
        {
            if (!property.CanWrite)
            {
                return;
            }

            if (columns.ContainsKey(header))
            {
                property.SetValue(newObj, SimpleConverter.ConvertStringToType(columns[header], property.PropertyType));
            }
            else
            {
                if (headerNameAttribute == null)
                {
                    throw new Exception(string.Format("No se encontro algun dato para el encabezado {0} en el archivo proporcionado.", header));
                }
                else
                {
                    if (headerNameAttribute.Important)
                    {
                        throw new Exception(string.Format("No se encontro algun dato para el encabezado {0} en el archivo proporcionado.", header));
                    }
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Asigna los valores proporcionados en las propiedades correspondientes a la instancia del objeto de tipo <typeparamref name="T"/>. Solo se aceptan valores originados desde un objeto anonimo o predefinidos del mismo tipo enviado.
        /// </summary>
        /// <typeparam name="T">El tipo del objeto a asignar.</typeparam>
        /// <param name="obj">El objeto a alimentarle los valores proporcionados.</param>
        /// <param name="values">Los valores usados en la asignacion de las propiedades. Se admiten objetos anonimos o predefinidos del mismo tipo enviado.</param>
        /// <returns>Regresa el objeto ya alimentado de los valores.</returns>
        public static T SetValuesIntoObjectOfType <T>(T obj, dynamic values)
        {
            PropertyInfo[] typeProperties      = typeof(T).GetProperties();
            PropertyInfo[] anonymousProperties = values.GetType().GetProperties();

            foreach (PropertyInfo typeProperty in typeProperties)
            {
                foreach (PropertyInfo anonymousProperty in anonymousProperties)
                {
                    if (typeProperty.Name.Equals(anonymousProperty.Name))
                    {
                        if (typeProperty.CanWrite)
                        {
                            typeProperty.SetValue(obj, SimpleConverter.ConvertStringToType(anonymousProperty.GetValue(values).ToString(), typeProperty.PropertyType));
                        }
                        else
                        {
                            throw new SetAccessorNotFoundException(typeProperty.Name);
                        }

                        break;
                    }
                }
            }

            return(obj);
        }
コード例 #4
0
        internal static T ConvertReaderToObjectOfType <T>(IDataReader reader, IEnumerable <PropertyInfo> properties) where T : Cope <T>, IManageable, new()
        {
            T newObj = new T();

            foreach (PropertyInfo property in properties)
            {
                property.SetValue(newObj, SimpleConverter.ConvertStringToType(reader[property.Name].ToString(), property.PropertyType));
            }

            return(newObj);
        }
コード例 #5
0
        /// <summary>
        /// Convierte un objeto de tipo System.Data.DataTable a un HashSet del tipo <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T">Tipo referencia para serializar.</typeparam>
        /// <param name="dataTable">El contenido a convertir.</param>
        /// <returns>Regresa un nuevo HashSet del tipo <typeparamref name="T"/> ya con los objetos incorporados.</returns>
        public static HashSet <T> ConvertDataTableToHashSetOfType <T>(System.Data.DataTable dataTable) where T : new()
        {
            HashSet <T> newSet = new HashSet <T>();

            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));
                    }
                }
                newSet.Add(newObject);
            }

            return(newSet);
        }
コード例 #6
0
        public static ICollection <object> ConvertDataTableToListOfType(System.Data.DataTable dataTable, Type target)
        {
            ICollection <object> newList = new List <object>();

            foreach (DataRow row in dataTable.Rows)
            {
                PropertyInfo[] properties = target.GetProperties();
                object         newObject  = Activator.CreateInstance(target);
                foreach (PropertyInfo property in properties)
                {
                    if (dataTable.Columns.Contains(property.Name) && property.CanWrite)
                    {
                        property.SetValue(newObject, SimpleConverter.ConvertStringToType(row[property.Name].ToString(), property.PropertyType));
                    }
                }
                newList.Add(newObject);
            }

            return(newList);
        }
コード例 #7
0
        /// <summary>
        /// Convierte un objeto de tipo System.Data.DataTable a un Diccionario con el tipo de la llave <typeparamref name="TKey"/> y valor <typeparamref name="TValue"/>.
        /// </summary>
        /// <typeparam name="TKey">El tipo que se usara como Llave.</typeparam>
        /// <typeparam name="TValue">El tipo que se usara como Valor.</typeparam>
        /// <param name="dataTable">El contenido a convertir.</param>
        /// <param name="keyName">El nombre de la columna dentro del objeto System.Data.DataTable.Columns que se usara como Llave.</param>
        /// <param name="valueName">El nombre de la columna dentro del objeto System.Data.DataTable.Columns que se usara como Valor.</param>
        /// <returns>Regresa un nuevo Diccionario alimentado con los valores proporcionados.</returns>
        public static Dictionary <TKey, TValue> ConvertDataTableToDictionary <TKey, TValue>(System.Data.DataTable dataTable, string keyName, string valueName)
        {
            if (dataTable.Columns.Count < 2)
            {
                throw new ArgumentException("Esta funcion requiere 2 columnas en el objeto System.Data.DataTable.");
            }

            Dictionary <TKey, TValue> newDictionary = new Dictionary <TKey, TValue>();
            TKey   key;
            TValue value;

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                key   = (TKey)SimpleConverter.ConvertStringToType(dataTable.Rows[i][keyName].ToString(), typeof(TKey));
                value = (TValue)SimpleConverter.ConvertStringToType(dataTable.Rows[i][valueName].ToString(), typeof(TValue));

                newDictionary.Add(key, value);
            }
            return(newDictionary);
        }
コード例 #8
0
        /// <summary>
        /// Convierte un objeto de tipo System.Data.DataTable a un Diccionario con el tipo de la llave <typeparamref name="TKey"/> y el objeto como valor.
        /// </summary>
        /// <typeparam name="TKey">El tipo que se usara como llave.</typeparam>
        /// <typeparam name="T">El tipo que se usara como objeto para el valor del diccionario.</typeparam>
        /// <param name="dataTable">El contenido a convertir.</param>
        /// <param name="keyName">El nombre de la columna dentro del objeto System.Data.DataTable.Columns que se usara como Llave.</param>
        /// <returns>Regresa un nuevo Diccionario alimentado con los valores proporcionados.</returns>
        public static Dictionary <TKey, T> ConvertDataTableToDictionary <TKey, T>(System.Data.DataTable dataTable, string keyName) where T : new()
        {
            Dictionary <TKey, T> newDictionary = new Dictionary <TKey, T>();

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

                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);
        }
コード例 #9
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);
        }