zoukankan      html  css  js  c++  java
  • 完美解决方案,可排除DATASET不支持System.Nullable错误

     完美解决方案,可排除DATASET不支持System.Nullable错误

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Reflection;
    using YTO.WeiXin.Model;
    using System.Collections;
    
    
    namespace YTO.WeiXin.Core
    {
        public class ListToDataTable
        {
            public static DataTable ToDataTable<T>(IList<T> list)
            {
                return ToDataTable(list, null);
            }
    
            //将list转换成DataTable
            public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)
            {
                List<string> propertyNameList = new List<string>();
                if (propertyName != null)
                {
                    propertyNameList.AddRange(propertyName);
                }
                DataTable result = new DataTable();
                if (list.Count > 0)
                {
                    PropertyInfo[] propertys = list[0].GetType().GetProperties();
                    foreach (PropertyInfo pi in propertys)
                    {
                        if (propertyNameList.Count == 0)
                        {
                            //if (DBNull.Value.Equals(pi.PropertyType))
                            //{
                            //   // pi.PropertyType = DateTime;
                            //}
                            Type colType = pi.PropertyType;
                            if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>))
                            {
                                colType = colType.GetGenericArguments()[0];
                            }
                            result.Columns.Add(pi.Name,colType);
                            //result.Columns.Add(pi.Name, pi.PropertyType);
                        }
                        else
                        {
                            if (propertyNameList.Contains(pi.Name))
                            {
                                result.Columns.Add(pi.Name, pi.PropertyType);
                            }
                        }
                    }
                    for (int i = 0; i < list.Count; i++)
                    {
                        ArrayList tempList = new ArrayList();
                        foreach (PropertyInfo pi in propertys)
                        {
                            if (propertyNameList.Count == 0)
                            {
                                object obj = pi.GetValue(list[i], null);
                                tempList.Add(obj);
                            }
                            else
                            {
                                if (propertyNameList.Contains(pi.Name))
                                {
                                    object obj = pi.GetValue(list[i], null);
                                    tempList.Add(obj);
                                }
                            }
                        }
                        object[] array = tempList.ToArray();
                        result.LoadDataRow(array, true);
                    }
                }
                return result;
            }
  • 相关阅读:
    动态规划5-多重背包
    动态规划4-完全背包
    利用dwebsocket在Django中使用Websocket
    Java学习笔记之:Spring MVC 环境搭建
    Struts2 国际化
    Java学习笔记之:Struts2.0 环境搭建
    LoadRunner:VuGen开发脚本步骤(二)
    LoadRunner:VuGen开发脚本步骤(一)
    Java学习笔记之:Java Servlet 过滤器配置
    Java学习笔记之:Java Servlet环境配置
  • 原文地址:https://www.cnblogs.com/slu182/p/4383673.html
Copyright © 2011-2022 走看看