惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - Franky
U
Unit 42
有赞技术团队
有赞技术团队
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
博客园 - 【当耐特】
罗磊的独立博客
B
Blog
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
爱范儿
爱范儿
The Cloudflare Blog
Engineering at Meta
Engineering at Meta
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MyScale Blog
MyScale Blog
阮一峰的网络日志
阮一峰的网络日志
H
Help Net Security
S
SegmentFault 最新的问题
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
L
LangChain Blog
T
Tailwind CSS Blog
WordPress大学
WordPress大学
GbyAI
GbyAI
Blog — PlanetScale
Blog — PlanetScale
V
Visual Studio Blog
Vercel News
Vercel News
Recent Announcements
Recent Announcements
博客园 - 司徒正美
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
Security Latest
Security Latest
T
Threat Research - Cisco Blogs

博客园 - 亦心

教你如何查找并下载某手某音上的外部视频 求出两个字符串中最大长度的相同的子字符串 年会抽奖软件 正则表达式+编码转换小工具 sql语句跨库导入导出 Google maps API开发(二) - 亦心 Google maps API开发(一) - 亦心 Javascript树型菜单(含源码) Javascript结合XML省市级联 - 亦心 - 博客园 ASP.NET程序读取二代身份证(附源码) - 亦心 - 博客园 仿百度搜索智能提示(纯JS实现) 发现一个免费申请国际域名的地方!! asp.net性能优化 分享一个分页存储过程和分页函数 任意类型转换成json tsql字符串操作 javascript正则表达式 - 亦心 - 博客园 access、excel取随机n条记录 测试SQL Server执行时间和CPU时间
利用反射实现通用的DataReader转List、DataReader转实体类
亦心 · 2009-12-28 · via 博客园 - 亦心

dataReader转list和转model有时候经常用,为了偷懒嘛,少写代码倒是不少,用缓存基本上也可以把性能补过来吧,反正我没有测试过,哪位同仁测试过,大家可以研究研究~~

把整理好的代码贴出来如下:大家可以参考参考   有点乱,注释就没有写了,地球人应该都看得懂,呵呵

 public static T ReaderToModel<T>(IDataReader dr)
    {
        
try
        {
            
using (dr)
            {
                
if (dr.Read())
                {
                    List<string> list = new List<string>(dr.FieldCount);
                    
for (int i = 0; i < dr.FieldCount; i++)
                    {
                        list.Add(dr.GetName(i).ToLower());
                    }
                    T model = Activator.CreateInstance<T>();
                    
foreach (PropertyInfo pi in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
                    {
                        
if (list.Contains(pi.Name.ToLower()))
                        {
                            
if (!IsNullOrDBNull(dr[pi.Name]))
                            {
                                pi.SetValue(model, HackType(dr[pi.Name], pi.PropertyType), null);
                            }
                        }
                    }
                    
return model;
                }
            }
            
return default(T);
        }
        
catch (Exception ex)
        {
            
throw ex;
        }
    }public static List<T> ReaderToList<T>(IDataReader dr)
    {
        
using (dr)
        {
            List<string> field = new List<string>(dr.FieldCount);
            
for (int i = 0; i < dr.FieldCount; i++)
            {
                field.Add(dr.GetName(i).ToLower());
            }
            List<T> list = new List<T>();
            
while (dr.Read())
            {
                T model = Activator.CreateInstance<T>();
                
foreach (PropertyInfo property in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
                {
                    
if (field.Contains(property.Name.ToLower()))
                    {
                        
if (!IsNullOrDBNull(dr[property.Name]))
                        {
                            property.SetValue(model, HackType(dr[property.Name], property.PropertyType), null);
                        }
                    }
                }
                list.Add(model);
            }
            
return list;
        }
    }
    //这个类对可空类型进行判断转换,要不然会报错
    
private static object HackType(object value, Type conversionType)
    {
        
if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
        {
            
if (value == null)
                
return null;            System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(conversionType);
            conversionType = nullableConverter.UnderlyingType;
        }
        
return Convert.ChangeType(value, conversionType);
    }private static bool IsNullOrDBNull(object obj)
    {
        
return ((obj is DBNull) || string.IsNullOrEmpty(obj.ToString())) ? true : false;
    }

每天进步一点点...