c# datatable 通过反射转成泛型list

在C#中,可以使用反射来将DataTable转换为泛型列表。下面是一个示例代码,展示了如何使用反射来实现这个转换过程:

using System;
using System.Collections.Generic;
using System.Data;

public class DataConverter
{
   
    public List<T> ConvertDataTableToList<T>(DataTable dataTable) where T : new()
    {
   
        List<T> list = new List<T>();

        foreach (DataRow row in dataTable.Rows)
        {
   
            T item = new T();

            foreach (DataColumn column in dataTable.Columns)
            {
   
                var property = typeof(T).GetProperty(column.ColumnName);
                if (property != null && row[column] != DBNull.Value)
                {
   
                    property.SetValue(item, Convert.ChangeType(row[column], property.PropertyType), null);
                }
            }

            list.Add(item);
        }

        return list;
    }
}

在这个示例中,我们创建了一个名为DataConverter的类,其中包含一个泛型方法ConvertDataTableToList<T>。这个方法接收一个DataTable作为参数,并使用反射技术将其转换为指定类型的泛型列表。在方法中,我们使用foreach循环遍历DataTable的行和列,然后使用反射来动态设置对象的属性值。

使用这个DataConverter类,可以很容易地将DataTable转换为任何指定类型的泛型列表。例如,假设我们有一个名为Person的实体类:

public class Person
{
   
    public int Id {
    get; set; }
    public string Name {
    get; set; }
    public int Age {
    get; set; }
}

可以这样使用DataConverter类来将DataTable转换为List<Person>

 // 创建一个DataTable实例
 DataTable dataTable = new DataTable("Person");

 // 添加列
 dataTable.Columns.Add("Id", typeof(int));
 dataTable.Columns.Add("Name", typeof(string));
 dataTable.Columns.Add("Age", typeof(int));

 // 添加行数据
 dataTable.Rows.Add(1, "John", 25);
 dataTable.Rows.Add(2, "Alice", 30);
 dataTable.Rows.Add(3, "Bob", 28);

 List<Person> personList = ConvertDataTableToList<Person>(dataTable);

在上面的示例中,我们根据Person类的属性结构,将DataTable中的数据转换为List<Person>。这种方法能够以通用的方式来转换任何实体类对应的DataTable为泛型列表,使代码更加灵活和可复用。

相关推荐

  1. c# datatable 通过反射list

    2024-02-01 06:38:03       61 阅读
  2. +反射

    2024-02-01 06:38:03       21 阅读
  3. JDBC封装,用反射实现.

    2024-02-01 06:38:03       67 阅读
  4. listtree返回前端

    2024-02-01 06:38:03       29 阅读
  5. <span style='color:red;'>泛</span><span style='color:red;'>型</span>..

    ..

    2024-02-01 06:38:03      61 阅读
  6. <span style='color:red;'>泛</span><span style='color:red;'>型</span>

    2024-02-01 06:38:03      27 阅读
  7. django orm中value和value_list以及list

    2024-02-01 06:38:03       27 阅读
  8. mybatis一对多查询,list中的是包装类

    2024-02-01 06:38:03       50 阅读
  9. 14 # 类与约束

    2024-02-01 06:38:03       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-01 06:38:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-01 06:38:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-01 06:38:03       87 阅读
  4. Python语言-面向对象

    2024-02-01 06:38:03       96 阅读

热门阅读

  1. Golang k8s相关yaml包的区别

    2024-02-01 06:38:03       48 阅读
  2. Spring Boot接收xml参数

    2024-02-01 06:38:03       59 阅读
  3. 【C++】三角形(triangle)

    2024-02-01 06:38:03       46 阅读
  4. Uni-app 如何上传文件, 使用的API是什么

    2024-02-01 06:38:03       59 阅读
  5. 华为网络设备:核心命令一览

    2024-02-01 06:38:03       53 阅读
  6. 设计模式全览:编程艺术的精髓!

    2024-02-01 06:38:03       48 阅读
  7. 设计模式——策略模式

    2024-02-01 06:38:03       47 阅读
  8. Spring Boot + Vue3 实现七牛云大视频上传

    2024-02-01 06:38:03       47 阅读
  9. 3D人体运动重建

    2024-02-01 06:38:03       68 阅读