예제 #1
0
        public static List <T> Map <T>(DataTable source) where T : class
        {
            List <T> mappedList = new List <T>();

            using (SqlDataMappingInfo <T> propMap = new SqlDataMappingInfo <T>())
            {
                try
                {
                    IEnumerable <PropertyInfo> properties = typeof(T).GetProperties();

                    if (properties != null && properties.Count() > 0)
                    {
                        properties = properties.Where(pi => pi.CanWrite);
                    }

                    if (properties == null || properties.Count() == 0)
                    {
                        return(null);
                    }

                    foreach (PropertyInfo prop in properties)
                    {
                        propMap.Add(prop, source.Columns);
                    }
                }
                catch (Exception) { }

                var constructor = new ConstructorDelegate <T>(() => (T)Activator.CreateInstance(typeof(T))); //CreateConstructor<T>();

                foreach (DataRow row in source.Rows)
                {
                    T map = constructor();// (T)Activator.CreateInstance(typeof(T));

                    #region set properties
                    foreach (SqlMappingInfo <T> mapping in propMap.Values) //(PropertyInfo propInfo in properties)
                    {
                        if (mapping?.SetValue == null || row.IsNull(mapping.ColumnNumber))
                        {
                            continue;
                        }

                        mapping.SetValue(map, row[mapping.ColumnNumber]);
                    }
                    #endregion set properties

                    mappedList.Add(map);
                }
            }
            return(mappedList);
        }
예제 #2
0
        public static List <T> Map <T>(SqlDataReader source) where T : class
        {
            List <T> mappedList = new List <T>();

            if (source == null || !source.HasRows)
            {
                return(mappedList);
            }
            //Dictionary<PropertyInfo, string> propertyMap = new Dictionary<PropertyInfo, string>();
            //Dictionary<PropertyInfo, Action<T, object>> setMap = new Dictionary<PropertyInfo, Action<T, object>>();
            using (SqlDataMappingInfo <T> propMap = new SqlDataMappingInfo <T>())
            {
                try
                {
                    string[] columns = new string[source.FieldCount];
                    for (int i = 0; i < source.FieldCount; ++i)
                    {
                        columns[i] = source.GetName(i).ToUpper().Trim();
                    }

                    IEnumerable <PropertyInfo> properties = typeof(T).GetProperties();

                    if (properties != null && properties.Count() > 0)
                    {
                        properties = properties.Where(pi => pi.CanWrite);
                    }


                    if (properties == null || properties.Count() == 0)
                    {
                        return(null);
                    }

                    foreach (PropertyInfo prop in properties)
                    {
                        propMap.Add(prop, columns);
                    }
                }
                catch (Exception) { }

                var constructor = new ConstructorDelegate <T>(() => (T)Activator.CreateInstance(typeof(T)));

                do
                {
                    while (source.Read())
                    {
                        T map = constructor(); //(T)Activator.CreateInstance(typeof(T));

                        #region set properties
                        foreach (SqlMappingInfo <T> mapping in propMap.Values) //(PropertyInfo propInfo in properties)
                        {
                            if (mapping?.SetValue == null || source.IsDBNull(mapping.ColumnNumber))
                            {
                                continue;
                            }

                            mapping.SetValue(map, source.GetValue(mapping.ColumnNumber));
                        }
                        #endregion set properties

                        mappedList.Add(map);
                    }
                } while (source.NextResult());
            }
            return(mappedList);
        }