欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#中利用委托反射将DataTable转换为实体集的代码

程序员文章站 2023-11-05 20:46:58
类泛型的约束: 复制代码 代码如下: public static class tomodel where t : class, new() 定义委托: 复...
类泛型的约束:
复制代码 代码如下:
public static class tomodel<t> where t : class, new()

定义委托:
复制代码 代码如下:
public delegate void setstring(string value);

创建委托方法:
复制代码 代码如下:

private static setstring createstringdelegate(t model, string propertyname)
{
methodinfo mi = model.gettype().getproperty(propertyname).getsetmethod();
type type = typeof(setstring);
return delegate.createdelegate(type, model, mi) as setstring;
}

利用反射和委托将datatable转换为实体集:
复制代码 代码如下:

public static ilist<t> getdelegate_tomodellist(datatable dt)
{
ilist<t> list = new list<t>();
if (dt == null || dt.rows.count < 1) return list;
setstring setdelegatestring;
foreach (datarow dr in dt.rows)
{
t model = new t();
foreach (datacolumn dc in dt.columns)
{
setdelegatestring = createstringdelegate(model, dc.columnname);
setdelegatestring(dr[dc.columnname].tostring());
}
list.add(model);
}
return list;
}

这样写问题就来了,因为委托定义的参数时string类型的,因为我们实体中可能有int或者datetime类型的,这时就需要用上泛型委托了
如果这样定义委托:
复制代码 代码如下:
public delegate void setstring<pt>(pt value)

创建委托方法(这里有问题,不知如何处理):
复制代码 代码如下:

private static setstring createstringdelegate(t model, string propertyname)
{
methodinfo mi = model.gettype().getproperty(propertyname).getsetmethod();
type type = typeof(model).getproperty(propertyname).propertytype;
return delegate.createdelegate(type, model, mi) as setstring<type>;
}

利用反射和委托将datatable转换为实体集:
复制代码 代码如下:

public static ilist<t> getdelegate_tomodellist(datatable dt)
{
ilist<t> list = new list<t>();
if (dt == null || dt.rows.count < 1) return list;
foreach (datarow dr in dt.rows)
{
t model = new t();
foreach (datacolumn dc in dt.columns)
{
setstring<typeof(t).getproperty(dc.columnname).propertytype> setdelegatestring = createstringdelegate(model, dc.columnname);
setdelegatestring(dr[dc.columnname].tostring());
}
list.add(model);
}
return list;
}

一直疑惑着,希望有人帮我解决疑惑,直接反射的方法我也有,但是这个问题不解决,心里一直有疙瘩,希望有人帮帮忙,谢谢
泛型可以动态构建的,你了解了这个,就能解决了,附上我的简略代码:
复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.data;
using system.reflection;
namespace rfttomodel {
class program {
static void main(string[] args) {
var result = tomodel<testmodel>.getdelegate_tomodellist(buildsampletable());
foreach (var item in result) {
console.writeline(item);
}
console.read();
}
static datatable buildsampletable() {
datatable result = new datatable();
result.columns.add("id", typeof(int));
result.columns.add("name", typeof(string));
result.columns.add("isdeleted", typeof(bool));
result.rows.add(new object[] { 1, "m.k", false });
result.rows.add(new object[] { 2, "b.g", true });
return result;
}
}
public class testmodel {
public int id { get; set; }
public string name { get; set; }
public bool isdeleted { get; set; }
public override string tostring() {
return string.format("id:{0} name:{1} isdeleted:{2}", id, name, isdeleted);
}
}
public delegate void setvalue<t>(t value);
public static class tomodel<t> where t : class, new() {
private static delegate createsetdelegate(t model, string propertyname) {
methodinfo mi = model.gettype().getproperty(propertyname).getsetmethod();
//这里构造泛型委托类型
type deltype = typeof(setvalue<>).makegenerictype(getpropertytype(propertyname));
return delegate.createdelegate(deltype, model, mi);
}
private static type getpropertytype(string propertyname) {
return typeof(t).getproperty(propertyname).propertytype;
}
public static ilist<t> getdelegate_tomodellist(datatable dt) {
ilist<t> list = new list<t>();
if (dt == null || dt.rows.count < 1) return list;
delegate setdelegate;
foreach (datarow dr in dt.rows) {
t model = new t();
foreach (datacolumn dc in dt.columns) {
setdelegate = createsetdelegate(model, dc.columnname);
//这里改变类型
setdelegate.dynamicinvoke(convert.changetype(dr[dc.columnname], getpropertytype(dc.columnname)));
}
list.add(model);
}
return list;
}
}
}

谢谢,我刚修改了,我传进去sqldatareader和datatable都可以转换了,当时只想着每次返回一个特定类型等委托都不知道如何下手,看着你的方法解决了
没想到dynamicinvoke这个方法,算是学习了,你的代码写着层次好清晰,看了是一种享受,向你学习啊!