zoukankan      html  css  js  c++  java
  • DataReader转换

    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);
            }
        }
  • 相关阅读:
    文件的权限
    Linux正则表达式
    Linux中硬链接与软链接
    Linux下文件属性介绍
    JavaScript核心之语法
    JavaScript概述
    浏览器播放wav语音文件,tomcat异常,ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。
    JS 数据结构-Set 集合 创建Set 常用Set方法
    JSON.parse 方法解析纯数字键值对报错的解决方法
    前端常用框架
  • 原文地址:https://www.cnblogs.com/shya/p/9302280.html
Copyright © 2011-2022 走看看