类泛型的约束:
复制代码 代码如下: 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这个方法,算是学习了,你的代码写着层次好清晰,看了是一种享受,向你学习啊!
【c#中利用委托反射将DataTable转换为实体集的代码】相关文章:
★ 一款域名监控小工具 Domain(IP)Watcher 实现代码
★ c# 冒泡排序算法(Bubble Sort) 附实例代码