zoukankan      html  css  js  c++  java
  • 利用反射实现通用的DataReader转List、DataReader转实体类

    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;
        }

    每天进步一点点...

  • 相关阅读:
    Linux系统下mysql修改密码遇到的问题
    Description: Field ud in com.yjj.service.impl.UserServiceImpl required a bean of type 'com.yjj.dao.UserDao' that could not be found. Action: Consider defining a bean of type 'com.yjj.dao.UserDao'
    地址的三级联动(jQuery+servlet)
    人生苦短,我用Python(目录)
    数据库增量同步
    并发编程
    Python Web 之 Flask
    jQuery 选择器
    常见性能优化方法的一些总结
    常见的浏览器兼容性问题总结
  • 原文地址:https://www.cnblogs.com/cyan/p/1634477.html
Copyright © 2011-2022 走看看