public class ListToDatatable
 {
     public static DataTable ToDataTable <T> (List <T> items)
     {
         var tb = new DataTable(typeof(T).Name);

         PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

         foreach(PropertyInfo prop in props)
         {
             Type t = GetCoreType(prop.PropertyType);
             tb.Columns.Add(prop.Name, t);
         }

         foreach(T item in items)
         {
             var values = new object[props.Length];

             ; i < props.Length; i++)
             {
                 values[i] = props[i].GetValue(item, null);
             }

             tb.Rows.Add(values);
         }

         return tb;
     }

     public static bool IsNullable(Type t)
     {
         return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable < > ));
     }

     public static Type GetCoreType(Type t)
     {
         if (t != null && IsNullable(t))
         {
             if (!t.IsValueType)
             {
                 return t;
             } else {
                 return Nullable.GetUnderlyingType(t);
             }
         } else {
             return t;
         }
     }
 }

最新文章

  1. Vertica参数的设置
  2. 导出excel报错
  3. Linq学习笔记(转)
  4. X32,X64,X86 代表什意义
  5. NOIP模拟赛-奶牛晒衣服(dry)
  6. Nginx 笔记与总结(12)Nginx URL Rewrite 实例(ecshop)
  7. Codeforce727B --- Bill Total Value(字符串处理 正则表达式)
  8. 【POJ】3415 Common Substrings
  9. JavaScript事件总结
  10. MyEclipse保存文件时 自动格式化代码! 不包括文档注释
  11. Delphi主窗口任务栏菜单的问题(转发WM_SYSCOMMAND到Application)
  12. [POI 2007]ZAP-Queries
  13. Linux网络技术管理及进程管理(week2_day4)--技术流ken
  14. SELECT 语句
  15. hadoop退出安全模式Name node is in safe mode
  16. face detection[FaceBoxes]
  17. kubernetes 简介:kube-dns 和服务发现
  18. [UE4]Transform镜面翻转
  19. MySQL查询表结构命令
  20. 关于Unity中的刚体和碰撞器的相关用法(一)

热门文章

  1. [四]基础数据概述之Byte详解
  2. angularjs1.X进阶笔记(3)——如何重构controller
  3. 学JAVA的第二天,静态网站制作,脑阔一点疼
  4. 我的第三个开源库GuaJiangViewDemo——中文文档
  5. 环境搭建 - Tomcat(Windows)
  6. document.getElementById(), getElementsByname(),getElementsByClassName(),getElementsByTagName()方法表示什么以及其意义
  7. vue2.5.2版本 :MAC设置应用在127.0.0.1:80端口访问; 并将127.0.0.1指向www.yours.com ;问题“ Invalid Host header”
  8. 一种提升连接Dynamics 365性能的方法
  9. C#:在匿名方法中捕获外部变量
  10. WPF开发为按钮提供添加,删除和重新排列ListBox内容的功能