一个实体类:
Class Model
{
public string ID{ get; set; }
public string Name{ get; set; }
}
一个DataTable:
ID_065 Name_0954
2 3
如果在类Model上 打上标记
Class Model
{
这是标记:ID_065
public string ID{ get; set; }
这是标记:Name_0954
public string Name{ get; set; }
}
这样DataTable转实体 形成的 model
就会使 ID 2 Name 3
万分感谢老板们 给出实例
你好 老板 我是这样写的 和他的类似 但是 我永远获取property属性为null
public class ColumnNameAttribute : Attribute
{
public string Name { get; set; }
public ColumnNameAttribute(string name)
{
this.Name = name;
}
}
class Model
{
[ColumnName("ID_065")]
public string id { get; set; }
[ColumnName("Name_0954")]
public string name { get; set; }
}
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID_065");
dt.Columns.Add("Name_0954");
dt.Rows.Add("ID_", "2");
dt.Rows.Add("Name_0", "3");
List<Model> models = MapToList<Model>(dt);
Console.ReadLine();
}
public static List<T> MapToList<T>(DataTable dataTable) where T : new()
{
List<T> entities = new List<T>();
foreach (DataRow row in dataTable.Rows)
{
T entity = new T();
foreach (DataColumn column in dataTable.Columns)
{
PropertyInfo property = entity.GetType().GetProperty(column.ColumnName);
if (property != null && property.CanWrite)
{
ColumnNameAttribute attribute = property.GetCustomAttribute<ColumnNameAttribute>();
if (attribute != null && attribute.Name == column.ColumnName)
{
property.SetValue(entity, row[column], null);
}
}
}
entities.Add(entity);
}
return entities;
}}
PropertyInfo property = entity.GetType().GetProperty(column.ColumnName);
永远为null
@22222220: 应该使用 .GetCustomAttribute<ColumnNameAttribute>()
@dudu: 这样获取的model数据也是null 我想映射进去 老板
下面是完整demo 您方便运行下吗
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID_065");
dt.Columns.Add("Name_0954");
dt.Rows.Add("ID_", "2");
dt.Rows.Add("Name_0", "3");
List<Model> models = MapToList<Model>(dt);
Console.ReadLine();
}
public static List<T> MapToList<T>(DataTable dataTable) where T : new()
{
List<T> entities = new List<T>();
foreach (DataRow row in dataTable.Rows)
{
T entity = new T();
foreach (DataColumn column in dataTable.Columns)
{
PropertyInfo property = entity.GetType().GetProperty(column.ColumnName);
if (property != null && property.CanWrite)
{
ColumnNameAttribute attribute = entity.GetType().GetCustomAttribute<ColumnNameAttribute>();
if (attribute != null && attribute.Name == column.ColumnName)
{
property.SetValue(entity, row[column], null);
}
}
}
entities.Add(entity);
}
return entities;
}}
class Model
{
[ColumnName("ID_065")]
public string id { get; set; }
[ColumnName("Name_0954")]
public string name { get; set; }
}
public class ColumnNameAttribute : Attribute
{
public string Name { get; set; }
public ColumnNameAttribute(string name)
{
this.Name = name;
}
}
}
已自行解决 谢谢老板
加了特性 找不到 PropertyInfo property = entity.GetType().GetProperty(column.ColumnName);
– 22222220 2年前