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 = ; 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()[];
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转换的更多相关文章

  1. 通过.net反射技术实现DataReader转换成Model实体类列表

     public static T ReaderToModel<T>(IDataReader dr) { try {  using (dr) {  if (dr.Read()) {  Typ ...

  2. c#将List转换成DataTable(采用Emit)

    前段时间通过网上查找,使用emit将Datatable,DataReader转换成List<T>了.这是从数据库到展示. 但是最近整理Hikari(我写的数据库连接池),发现c#里面数据库 ...

  3. linq世界走一走(LINQ TO SQL)

    前言:作为linq的一个组件,同时作为ADO.NET的一个组成部分,LINQ TO SQL提供了将关系数据映射为对象的运行时基础结构. LINQ TO SQL是通过将关系数据库对象的数据模型(如一个数 ...

  4. c# 轻量级ORM框架 实现(一)

    发布一个自己写的一个轻量级ORM框架,本框架设计期初基于三层架构.所以从命名上来看,了解三层的朋友会很好理解. 设计该框架的目的:不想重复的写增删改查,把精力放到功能实现上. 发布改框架的原因:希望给 ...

  5. 利用npoi把多个DataTable导入Excel多个sheet中

    { 题外拓展:把datatable插入dataset DataTable fuben = new DataTable();//定义的datatablefuben = table.Tables[0].C ...

  6. 反射实现 Data To Model

    调用 : public ActionResult Index() { DataTable dt = new DataTable(); dt.Columns.Add("Name"); ...

  7. MVC仓储类Repository

    接口: using Common; using System; using System.Collections; using System.Collections.Generic; using Sy ...

  8. C#通用数据访问类库

    说明:此篇文章是给那些和我一样仍在使用ADO.NET访问数据库的.NET开发人员写的,因为某些原因,比如还在使用.NET3.0以下版本开发.NET应用或者所使用的数据库对ORM支持不是很好,或者是对O ...

  9. c#随便聊聊数据库操作

    最近在学习web后台以及Python,到了程序员的转折年纪了,哎.估计很久不会写博文了.言归正传. 在原理的数据库连接池HiKari项目上.我扩展了独立的3个库,说是3个库,其实原本该是一个库.先聊聊 ...

随机推荐

  1. Java 获取操作系统相关的内容

    package com.hikvision.discsetup.util; import java.lang.reflect.Field; import java.net.InetAddress; i ...

  2. awk文本处理

    一.前言 (一).awk简介 awk是一种编程语言,用于在linux/unix下对文本和数据进行处理,数据可以来自标准输入.一个或多个文件,或其它命令的输出,它支持用户自定义函数和动态正则表达式等先进 ...

  3. js中判断一个对象的类型的种种方法

    javascript中检测对象的类型的运算符有:typeof.constructor.instanceof. typeof:typeof是一个一元运算符,返回结果是一个说明运算数类型的字符串.如:&q ...

  4. 201312-2ISBN号码

    问题描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上的减号),最后一位 ...

  5. mvnjar包冲突解决方法

    命令 mvn dependency:tree -Dverbose 结果: [INFO] +- com.esotericsoftware:kryo:jar:4.0.2:test [INFO] | +- ...

  6. 集成方法 Ensemble

    一.bagging 用于基础模型复杂.容易过拟合的情况,用来减小 variance(比如决策树).基础模型之间没有太多联系(相对于boosting),训练可以并行.但用 bagging 并不能有助于把 ...

  7. Javascript中,实现十大排序方法之一(冒泡排序及其优化设想)

    冒泡排序的Javascript实现 首先定义一个取值范围在(0~100000)之间的随机值的长度为10万的数组, function bubbleSort(arr) { console.time('冒泡 ...

  8. keras 学习笔记:从头开始构建网络处理 mnist

    全文参考 < 基于 python 的深度学习实战> import numpy as np from keras.datasets import mnist from keras.model ...

  9. React预备知识点

    1.react中的状态提升 react的状态提升就是用户对子组件操作,子组件不改变自己的状态,而是通过自己的props把操作改变的数据传递给父组件,改变父组件的状态,从而改变受父组件控制的所有子组件的 ...

  10. DC6-靶机渗透

    靶场下载链接: Download: http://www.five86.com/downloads/DC-6.zip Download (Mirror): https://download.vulnh ...