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;
            }
  • 相关阅读:
    VMware提示此主机支持Intel VT-x,但Intel VT-x处于禁用状态怎么解决
    linux中几种安装软件 方法
    Linux向文件添加内容的几种方法
    什么是模块?模块划分的原则是什么?
    NOIP2009 t3 最优贸易
    HDU3072 Intelligence System
    洛谷P2569 股票交易
    玄学
    [0403]学习一个——苟(简单Java开发)
    实验 3:类和对象
  • 原文地址:https://www.cnblogs.com/slu182/p/4383673.html
Copyright © 2011-2022 走看看