为大家介绍在c#编程中,如何使用委托反射把datatable转换为实体集的方法,有需要的朋友可以参考下。
1、类泛型的约束:
复制代码 代码示例:
public static class ToModel<T> where T : class, new()
2、定义委托:
复制代码 代码示例:
public delegate void SetString(string value);
3、创建委托方法:
复制代码 代码示例:
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;
}
4、借助反射和委托将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类型的,这时就需要用上泛型委托了。
5、定义委托:
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>;
}
6、利用反射和委托将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;
}
7、泛型可以动态构建的,简略代码:
复制代码 代码示例:
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这个方法。