当前位置:网站首页>C# DataTable转换为Entity(反射&&泛型)
C# DataTable转换为Entity(反射&&泛型)
2022-06-21 20:41:00 【zLulus】
public static IEnumerable<T> Parse<T>(IEnumerable<DataRow> rows) where T : class, new()
{
if (rows == null || Enumerable.FirstOrDefault<DataRow>(rows) == null)
return (IEnumerable<T>) new T[0];
PropertyInfo[] properties = typeof (T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty);
List<T> list = new List<T>();
foreach (DataRow row in rows)
{
T instance = Activator.CreateInstance<T>();
DbHelper.Parse((object) instance, (IEnumerable<PropertyInfo>) properties, row);
list.Add(instance);
}
return (IEnumerable<T>) list;
}
private static void Parse(object obj, IEnumerable<PropertyInfo> properties, DataRow row)
{
foreach (PropertyInfo propertyInfo in properties)
{
if (DataRowExtension.HasValue(row, propertyInfo.Name))
{
try
{
propertyInfo.SetValue(obj, DbHelper.ConvertType(CultureInfo.CurrentCulture, row[propertyInfo.Name], propertyInfo.PropertyType), (object[]) null);
}
}
catch{ }
}
}
边栏推荐
- 【深入理解TcaplusDB技术】TcaplusDB构造数据
- Implement a middleware from -1
- Notes on topic brushing (16) -- binary tree: modification and construction
- 【深入理解TcaplusDB技术】一键安装Tmonitor后台
- Pi4j pin analog bus, several ideas of control transmission and data transmission
- MitoZ|Multi-Kmer mode
- Pi4j GPIO pin pull-up resistance, pull-down resistance concept
- 力扣刷題集結4(mysql版本)
- Leetcode question brushing: SF Technology Smart logistics Campus Technology Challenge
- flutter系列之:flutter中的IndexedStack
猜你喜欢
随机推荐
【LeetCode】8、字符串转换整数(atoi)
An example of CPU instruction rearrangement leading to errors
GDB debugging practice (7) signal processing
关于lg(n!)的渐进紧确界
采样器合集
Verilog除法器的设计总结
【深入理解TcaplusDB技術】TcaplusDB構造數據
如何卸载用conda命令安装的包
从-1开始实现一个中间件
Pi4j pin analog bus, several ideas of control transmission and data transmission
LeetCode-543-二叉树的直径
Use the for loop to calculate n! Value of
GDB调试实战(8)给程序传递启动参数
Notes on topic brushing (16) -- binary tree: modification and construction
001 new construction project based on opencvsharp
GDB debugging skills (0) getting started with GDB
PAML|计算dN/dS值的生信软件
Use the do while loop to calculate the odd and even sums in 1-100 [method 1]
MySql踩坑记录
Communication failure between botu simulation HMI and real 1200plc









