反射实现 DataTable 转对象集合(一般场景通用)

最终效果

反射实现 DataTable 转对象集合(一般场景通用)_第1张图片

Demo结构

反射实现 DataTable 转对象集合(一般场景通用)_第2张图片

测试代码

实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataTableToEntitys
{
    public class TestEntity
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }

        public override string ToString()
        {
            return "Id:" + Id + "Name:" + Name + "Age:" + Age;
        }
    }
}

扩展类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DataTableToEntitys
{
    public static partial class ConvertEx
    {
        /// 
        /// (扩展)使用反射技术DataTable转List
        /// 
        /// 
        /// 
        /// 
        public static List ToEntitys(this DataTable dataTable) where T : class, new()
        {
            var lists = new List();
            foreach (DataRow item in dataTable.Rows)
            {
                T t = new T();
                // 获得此模型的公共属性
                PropertyInfo[] propertys = t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    if (dataTable.Columns.Contains(pi.Name))
                    {
                        // 判断此属性是否有Setter
                        if (!pi.CanWrite) continue;
                        object value = item[pi.Name];  //取值

                        if (value != DBNull.Value) //如果非空,则赋给对象的属性
                        {
                            value = Convert.ChangeType(value.ToString(), pi.PropertyType);
                            pi.SetValue(t, value, null);//类型转换。
                        }
                    }
                }
                lists.Add(t);
            };
            return lists;
        }
    }
}

测试代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DataTableToEntitys
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            DataTable dataTable = GetDateTables();
            List entities = dataTable.ToEntitys();
            foreach (var item in entities)
            {
                Console.WriteLine(item.ToString());
            }
            Console.ReadKey();
        }

        /// 
        /// 模拟返回DataTable数据
        /// 
        /// 
        public static DataTable GetDateTables()
        {
            DataTable table = new DataTable();
            //创建table的第一列
            DataColumn id = new DataColumn();
            // 该列的数据类型
            id.DataType = Type.GetType("System.Int32");
            //该列得名称
            id.ColumnName = "id";
            //该列得默认值
            id.DefaultValue = 1;

            //第二列
            DataColumn name = new DataColumn();
            name.DataType = Type.GetType("System.String");
            name.ColumnName = "name";
            name.DefaultValue = "张三";

            //第三列
            DataColumn age = new DataColumn();
            age.DataType = Type.GetType("System.Int32");
            age.ColumnName = "age";
            age.DefaultValue = 18;

            // 将所有的列添加到table上
            table.Columns.Add(id);
            table.Columns.Add(name);
            table.Columns.Add(age);

            //创建一行
            DataRow row = table.NewRow();
            //将此行添加到table中
            table.Rows.Add(row);
            return table;
        }
    }
}

 

你可能感兴趣的:(.Net)