码迷,mamicode.com
首页 > 其他好文 > 详细

DataReader转换

时间:2018-07-12 23:51:40      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:flags   item   inf   oob   current   转换   code   dbconnect   command   

public static partial class Extension
    {
        
        private static ConcurrentDictionary<Type, ConcurrentDictionary<string, PropertyInfo>> typePropertyCache = new ConcurrentDictionary<Type, ConcurrentDictionary<string, PropertyInfo>>();

        private static ConcurrentDictionary<string, PropertyInfo> GetTypePropertyMap(Type entityType)
        {
            if (typePropertyCache.ContainsKey(entityType))
                return typePropertyCache[entityType];
            ConcurrentDictionary<string, PropertyInfo> propertyMappers = new ConcurrentDictionary<string, PropertyInfo>();
            var properties = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
            foreach (var item in properties)
            {
                string columnName = ImprovedNamingStrategy.Instance.PropertyToColumnName(item.Name);
                propertyMappers.TryAdd(columnName, item);
            }
            typePropertyCache[entityType] = propertyMappers;
            return propertyMappers;
        }


        public static IEnumerable<T> QueryList<T>(this IDbConnection connection, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            IDataReader reader = connection.ExecuteReader(sql, param, transaction, commandTimeout, commandType);

            List<T> list = new List<T>();
            var propertyMappers = GetTypePropertyMap(typeof(T));
            while (reader.Read())
            {
                T obj = Activator.CreateInstance<T>();

                for (int i = 0; i < reader.FieldCount; i++)
                {
                    if (reader.IsDBNull(i))
                    {
                        continue;
                    }
                    string columnName = reader.GetName(i).ToLower();
                    if (!propertyMappers.ContainsKey(columnName))
                    {
                        continue;
                    }
                    PropertyInfo property = propertyMappers[columnName];
                    if (property == null)
                    {
                        continue;
                    }

                    property.SetValue(obj, ChangeType(reader[columnName], property.PropertyType));
                }
                list.Add(obj);
            }
            return list;
        }

        static public object ChangeType(object value, Type type)
        {
            if (value == null && type.IsGenericType) return Activator.CreateInstance(type);
            if (value == null) return null;
            if (type == value.GetType()) return value;
            if (type.IsEnum)
            {
                if (value is string)
                    return Enum.Parse(type, value as string);
                else
                    return Enum.ToObject(type, value);
            }
            if (!type.IsInterface && type.IsGenericType)
            {
                Type innerType = type.GetGenericArguments()[0];
                object innerValue = ChangeType(value, innerType);
                return Activator.CreateInstance(type, new object[] { innerValue });
            }
            if (value is string && type == typeof(Guid)) return new Guid(value as string);
            if (value is string && type == typeof(Version)) return new Version(value as string);
            if (!(value is IConvertible)) return value;
            return Convert.ChangeType(value, type);
        }
    }

 

DataReader转换

标签:flags   item   inf   oob   current   转换   code   dbconnect   command   

原文地址:https://www.cnblogs.com/shya/p/9302280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!