IDataReader转成List<T>


/// <summary>
/// IDataReader2List
/// </summary>
public List<T> ExecuteList<T>(string sql, T type) where T : class
{
    List<T> list = new List<T>();
    using (IDataReader reader = ExecuteReader(sql))
    {
        PropertyInfo[] props = typeof(T).GetProperties();
        object[] args = new object[props.Length];
        bool[] isNullableAry = GetIsNullable(props);
        int[] index = GetFields(props, reader);
        Type nullableType = typeof(Nullable<>);
        Type DBNullType = typeof(DBNull);

        while (reader.Read())
        {
            for (int j = 0; j < reader.FieldCount; j++)
            {
                int i = index[j];
                if (i == -1) continue;
                object value = reader.GetValue(j);
                Type valueType = value.GetType();
                if (object.ReferenceEquals(valueType, DBNullType))
                {
                    args[i] = null;
                    continue;
                }

                Type propType = props[i].PropertyType;
                if (isNullableAry[i])
                {
                    Type originalType = propType.GetGenericArguments()[0];
                    Type genericType = nullableType.MakeGenericType(originalType);
                    args[i] = Activator.CreateInstance(genericType, Convert.ChangeType(value, originalType));
                    continue;
                }

                args[i] = Convert.ChangeType(value, propType);
            }
            object instance = Activator.CreateInstance(typeof(T), args);
            list.Add(instance as T);
        }
    }
    return list;
}

/// <summary>
/// 获取匿名类型字段类型是否为可空类型
/// </summary>
private bool[] GetIsNullable(PropertyInfo[] infos)
{
    bool[] NullableAry = new bool[infos.Length];
    Type nullable = typeof(Nullable<>);
    for (int i = 0; i < infos.Length; i++)
    {
        var item = infos[i].PropertyType;
        if (item.IsValueType && item.IsGenericType)
        {
            var definition = item.GetGenericTypeDefinition();
            if (definition != null && object.ReferenceEquals(definition, nullable))
            {
                NullableAry[i] = true;
            }
        }
    }
    return NullableAry;
}

/// <summary>
/// 获取数据库种字段对应的顺序
/// </summary>
private int[] GetFields(PropertyInfo[] infos, IDataReader reader)
{
    int[] index = new int[reader.FieldCount];
    for (int i = 0; i < index.Length; i++)
    {
        index[i] = -1;
    }
    for (int i = 0; i < infos.Length; i++)
    {
        for (int j = 0; j < reader.FieldCount; j++)
        {
            if (infos[i].Name.ToLower() == reader.GetName(j).ToLower())
            {
                index[j] = i;
                break;
            }
        }
    }
    return index;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值