Hidistro.Entities/ReaderConvert.cs

(开头部分) 4KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

namespace Hidistro.Entities
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Reflection;

    public class ReaderConvert
    {
        private static object CheckType(object value, Type conversionType)
        {
            if (value == null)
            {
                return null;
            }
            return Convert.ChangeType(value, conversionType);
        }

        private static bool IsNullOrDBNull(object obj)
        {
            return ((obj == null) || (obj is DBNull));
        }

        public static IList<T> ReaderToList<T>(IDataReader objReader) where T: new()
        {
            if (objReader != null)
            {
                List<T> list = new List<T>();
                Type type = typeof(T);
                while (objReader.Read())
                {
                    T local2 = default(T);
                    T local = (local2 == null) ? Activator.CreateInstance<T>() : (local2 = default(T));
                    for (int i = 0; i < objReader.FieldCount; i++)
                    {
                        if (!IsNullOrDBNull(objReader[i]))
                        {
                            PropertyInfo property = type.GetProperty(objReader.GetName(i).Replace("_", ""), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                            if (property != null)
                            {
                                Type propertyType = property.PropertyType;
                                if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                                {
                                    NullableConverter converter = new NullableConverter(propertyType);
                                    propertyType = converter.UnderlyingType;
                                }
                                if (property.PropertyType.IsEnum)
                                {
                                    object obj2 = Enum.ToObject(propertyType, objReader[i]);
                                    property.SetValue(local, obj2, null);
                                }
                                else
                                {
                                    property.SetValue(local, CheckType(objReader[i], propertyType), null);
                                }
                            }
                        }
                    }
                    list.Add(local);
后面还有 45 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。