C#帮助类:将List转换成Datatable
2024-10-09 23:56:56
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; } } }
最新文章
- Vertica参数的设置
- 导出excel报错
- Linq学习笔记(转)
- X32,X64,X86 代表什意义
- NOIP模拟赛-奶牛晒衣服(dry)
- Nginx 笔记与总结(12)Nginx URL Rewrite 实例(ecshop)
- Codeforce727B --- Bill Total Value(字符串处理 正则表达式)
- 【POJ】3415 Common Substrings
- JavaScript事件总结
- MyEclipse保存文件时 自动格式化代码! 不包括文档注释
- Delphi主窗口任务栏菜单的问题(转发WM_SYSCOMMAND到Application)
- [POI 2007]ZAP-Queries
- Linux网络技术管理及进程管理(week2_day4)--技术流ken
- SELECT 语句
- hadoop退出安全模式Name node is in safe mode
- face detection[FaceBoxes]
- kubernetes 简介:kube-dns 和服务发现
- [UE4]Transform镜面翻转
- MySQL查询表结构命令
- 关于Unity中的刚体和碰撞器的相关用法(一)
热门文章
- [四]基础数据概述之Byte详解
- angularjs1.X进阶笔记(3)——如何重构controller
- 学JAVA的第二天,静态网站制作,脑阔一点疼
- 我的第三个开源库GuaJiangViewDemo——中文文档
- 环境搭建 - Tomcat(Windows)
- document.getElementById(), getElementsByname(),getElementsByClassName(),getElementsByTagName()方法表示什么以及其意义
- vue2.5.2版本 :MAC设置应用在127.0.0.1:80端口访问; 并将127.0.0.1指向www.yours.com ;问题“ Invalid Host header”
- 一种提升连接Dynamics 365性能的方法
- C#:在匿名方法中捕获外部变量
- WPF开发为按钮提供添加,删除和重新排列ListBox内容的功能