应用场景:
需要对接多台设备,且数据结构都不一致,实体类都不一致,但是需要调用相同接口去实现, 需要将DataTable转集合对象,在由集合对象转JSON字符串 传给api。于是在这一步我设计成 在实体类上面 加上特性(这个字段特性与查询出来DataTable字段一致就行了)
具体实现代码
实体类
关键 Description 这个特性值需要和dataTable字段值一直
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DataTableToEntitys { public class TestEntity { [Description("字段1")] public int Id { get; set; } [Description("字段2")] public string Name { get; set; } [Description("字段3")] 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 { public static List<T> ToEntitys<T>(this DataTable dataTable) where T : class, new() { var lists = new List<T>(); foreach (DataRow item in dataTable.Rows) { T t = new T(); // 获得此模型的公共属性 PropertyInfo[] propertys = t.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { //获取属性上面特性 DescriptionAttribute descriptionAttribute = new DescriptionAttribute(); object[] objAttr = pi.GetCustomAttributes(descriptionAttribute.GetType(), true); if (objAttr.Length > 0) { //获取特性值 var attrValue = ((DescriptionAttribute)objAttr[0]).Description; if (dataTable.Columns.Contains(attrValue)) { // 判断此属性是否有Setter if (!pi.CanWrite) continue;//该属性不可写,直接跳出 object value = item[attrValue]; //取值 //如果非空,则赋给对象的属性 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<TestEntity> entities = dataTable.ToEntitys<TestEntity>(); foreach (var item in entities) { Console.WriteLine(item.ToString()); } Console.ReadKey(); } /// <summary> /// 模拟返回DataTable数据 /// </summary> /// <returns></returns> public static DataTable GetDateTables() { DataTable table = new DataTable(); //创建table的第一列 DataColumn id = new DataColumn(); // 该列的数据类型 id.DataType = Type.GetType("System.Int32"); //该列得名称 id.ColumnName = "字段1"; //该列得默认值 id.DefaultValue = 1; //第二列 DataColumn name = new DataColumn(); name.DataType = Type.GetType("System.String"); name.ColumnName = "字段2"; name.DefaultValue = "张三"; //第三列 DataColumn age = new DataColumn(); age.DataType = Type.GetType("System.Int32"); age.ColumnName = "字段3"; 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; } } }结果
