• DataTable转换成匿名类的List类型


    因为匿名类是不能够 Activator.CreateInstance进行反射实例化的

    /// <summary>
            /// 匿名类的转换方式
            /// </summary>
            /// <param name="GenericType"></param>
            /// <param name="dataTable"></param>
            /// <returns></returns>
            public static IList FromTable(Type GenericType, DataTable dataTable)
            {
                Type typeMaster = typeof(List<>);
                Type listType = typeMaster.MakeGenericType(GenericType);
                IList list = Activator.CreateInstance(listType) as IList;
                if (dataTable == null || dataTable.Rows.Count == 0)
                    return list;
                var constructor = GenericType.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                               .OrderBy(c => c.GetParameters().Length).First();
                var parameters = constructor.GetParameters();
                var values = new object[parameters.Length];
                foreach (DataRow dr in dataTable.Rows)
                {
                    int index = 0;
                    foreach (ParameterInfo item in parameters)
                    {
                        object itemValue = null;
                        if (dr[item.Name] != null && dr[item.Name] != DBNull.Value)
                        {
                            itemValue = Convert.ChangeType(dr[item.Name], item.ParameterType.GetUnderlyingType());
                        }
                        values[index++] = itemValue;
                    }
                    list.Add(constructor.Invoke(values));
                }
                return list;
            }
    
            /// <summary>
            /// 匿名类的转换方式
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="dataTable"></param>
            /// <returns></returns>
            public static List<T> FromTable<T>(DataTable dataTable)
            {
                List<T> list = new List<T>();
                if (dataTable == null || dataTable.Rows.Count == 0)
                    return list;
                //取当前匿名类的构造函数
                var constructor = typeof(T).GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                               .OrderBy(c => c.GetParameters().Length).First();
                //取当前构造函数的参数
                var parameters = constructor.GetParameters();
                var values = new object[parameters.Length];
                foreach (DataRow dr in dataTable.Rows)
                {
                    int index = 0;
                    foreach (ParameterInfo item in parameters)
                    {
                        object itemValue = null;
                        if (dr[item.Name] != null)
                        {
                            itemValue = Convert.ChangeType(dr[item.Name], item.ParameterType.GetUnderlyingType());
                        }
                        values[index++] = itemValue;
                    }
                    T entity = (T)constructor.Invoke(values);
                    list.Add(entity);
                }
                return list;
            }

    使用方法

     var entity = new { Name = "item", ID = 0, GuidType = Guid.Empty };
    
    
                DataTable dataTable = new DataTable();
                dataTable.Columns.Add("Name", typeof(string));
                dataTable.Columns.Add("ID", typeof(int));
                dataTable.Columns.Add("GuidType", typeof(Guid));
    
                for (int i = 0; i < 10; i++)
                {
                    DataRow dr = dataTable.NewRow();
                    dr["Name"] = "STRING" + i;
                    dr["ID"] = i;
                    if (i % 2 == 0)
                        dr["GuidType"] = Guid.Empty;
                    else
                    {
                        dr["GuidType"] = DBNull.Value;
                    }
                    dataTable.Rows.Add(dr);
                }
    
                IList list = EntityExtensions.FromTable(entity.GetType(), dataTable);
    千人.NET交流群:18362376,因为有你,代码变得更简单,加群请输入cnblogs
  • 相关阅读:
    测试及开发中应当要考虑的安全问题
    31、Python之会话管理cookie和session
    Scrapy爬虫框架入门
    30、Python之web框架django进阶篇
    装饰器函数
    函数初始后续与进阶
    函数初识
    文件操作
    日本语自学
    编码,集合
  • 原文地址:https://www.cnblogs.com/kingkoo/p/2543259.html
Copyright © 2020-2023  润新知