c# 隐含类型var 转换为 DataTable

/// <summary>

/// 隐含类型var 转换为 DataTable

/// </summary>

/// <typeparam name="T"></typeparam>

/// <param name="array"></param>

/// <returns></returns>

public static DataTable CopyToDataTable<T>(this IEnumerable<T> array)

{

var ret = new DataTable();

foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))

ret.Columns.Add(dp.Name, dp.PropertyType);

foreach (T item in array)

{

var Row = ret.NewRow();

foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))

Row[dp.Name] = dp.GetValue(item);

ret.Rows.Add(Row);

}

return ret;

}

}

相关推荐
疯狂的喵43 分钟前
C++编译期多态实现
开发语言·c++·算法
2301_765703141 小时前
C++中的协程编程
开发语言·c++·算法
m0_748708051 小时前
实时数据压缩库
开发语言·c++·算法
lly2024062 小时前
jQuery Mobile 表格
开发语言
惊讶的猫2 小时前
探究StringBuilder和StringBuffer的线程安全问题
java·开发语言
m0_748233172 小时前
30秒掌握C++核心精髓
开发语言·c++
Fleshy数模3 小时前
从数据获取到突破限制:Python爬虫进阶实战全攻略
java·开发语言
Duang007_3 小时前
【LeetCodeHot100 超详细Agent启发版本】字母异位词分组 (Group Anagrams)
开发语言·javascript·人工智能·python
froginwe113 小时前
Redis 管道技术
开发语言
u0109272713 小时前
C++中的RAII技术深入
开发语言·c++·算法