Exemplo n.º 1
0
        /// <summary>
        /// DataTable'ı List'e çevirir
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public static List <T> ConvertDataTableToList <T>(this object dataTable) where T : class, new()
        {
            DataTable dt         = dataTable as DataTable;
            List <T>  arr        = new List <T>();
            Type      entityType = typeof(T);

            PropertyInfo[] properties = entityType.GetProperties();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                T obj = new T();
                foreach (PropertyInfo property in properties)
                {
                    ColumnMapperAttribute mapperAttribute = property.GetCustomAttributes(typeof(ColumnMapperAttribute), true).Select((o) => o as ColumnMapperAttribute).FirstOrDefault();
                    string propertyName = mapperAttribute == null ? property.Name : mapperAttribute.HeaderName;

                    dynamic value = dt.Rows[i][propertyName].ToString();

                    //var converter = TypeDescriptor.GetConverter(property.PropertyType);
                    //var result = converter.ConvertFrom(value);
                    if (dt.Rows[i][propertyName].ToString() != "")
                    {
                        var converter = TypeDescriptor.GetConverter(property.PropertyType);
                        var result    = converter.ConvertFrom(value);
                        //var result = Convert.ChangeType(value, property.PropertyType);
                        obj.GetType().GetProperty(property.Name).SetValue(obj, result, null);
                    }
                    else if (property.PropertyType == typeof(string))
                    {
                        obj.GetType().GetProperty(property.Name).SetValue(obj, "", null);
                    }
                }
                arr.Add(obj);
            }
            return(arr);
        }
Exemplo n.º 2
0
        /// <summary>
        /// DataTable'ı ObjectEe çevirir
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public static T ConvertDataTableToSingle <T>(this object dataTable) where T : class, new()
        {
            DataTable dt         = dataTable as DataTable;
            Type      entityType = typeof(T);

            PropertyInfo[] properties = entityType.GetProperties();
            T obj = new T();

            foreach (PropertyInfo property in properties)
            {
                ColumnMapperAttribute mapperAttribute = property.GetCustomAttributes(typeof(ColumnMapperAttribute), true).Select((x) => x as ColumnMapperAttribute).FirstOrDefault();
                string  propertyName = mapperAttribute == null ? property.Name : mapperAttribute.HeaderName;
                dynamic value        = dt.Rows[0][propertyName].ToString();

                var converter = TypeDescriptor.GetConverter(property.PropertyType);
                var result    = converter.ConvertFrom(value);

                obj.GetType().GetProperty(property.Name).SetValue(obj, result, null);
            }
            return(obj);
        }