DataTable转实体Model,DataRow转实体Model,DataTable转泛型T,DataRow转泛型T

前言,此方法利用反射将DataRow转成实体,由于反射性能不行,大家就看看就行了吧。

代码来啦
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text; namespace WangSql.DBUtility
{
public class DataMapHelper
{
private enum ModelType
{
//值类型
Struct,
Enum, //引用类型
String,
Object,
Else
}
private static ModelType GetModelType(Type modelType)
{
if (modelType.IsEnum)//值类型
{
return ModelType.Enum;
}
if (modelType.IsValueType)//值类型
{
return ModelType.Struct;
}
else if (modelType == typeof(string))//引用类型 特殊类型处理
{
return ModelType.String;
}
else if (modelType == typeof(object))//引用类型 特殊类型处理
{
return ModelType.Object;
}
else//引用类型
{
return ModelType.Else;
}
} public static List<T> DataTableToList<T>(DataTable table)
{
List<T> list = new List<T>();
foreach (DataRow item in table.Rows)
{
list.Add(DataRowToModel<T>(item));
}
return list;
}
public static T DataRowToModel<T>(DataRow row)
{
T model;
Type type = typeof(T);
ModelType modelType = GetModelType(type);
switch (modelType)
{
case ModelType.Struct://值类型
{
model = default(T);
if (row[] != null)
model = (T)row[];
}
break;
case ModelType.Enum://值类型
{
model = default(T);
if (row[] != null)
{
Type fiType = row[].GetType();
if (fiType == typeof(int))
{
model = (T)row[];
}
else if (fiType == typeof(string))
{
model = (T)Enum.Parse(typeof(T), row[].ToString());
}
}
}
break;
case ModelType.String://引用类型 c#对string也当做值类型处理
{
model = default(T);
if (row[] != null)
model = (T)row[];
}
break;
case ModelType.Object://引用类型 直接返回第一行第一列的值
{
model = default(T);
if (row[] != null)
model = (T)row[];
}
break;
case ModelType.Else://引用类型
{
model = System.Activator.CreateInstance<T>();//引用类型 必须对泛型实例化
#region MyRegion
//获取model中的属性
PropertyInfo[] modelPropertyInfos = type.GetProperties();
//遍历model每一个属性并赋值DataRow对应的列
foreach (PropertyInfo pi in modelPropertyInfos)
{
//获取属性名称
String name = pi.Name;
if (row.Table.Columns.Contains(name) && row[name] != null)
{
ModelType piType = GetModelType(pi.PropertyType);
switch (piType)
{
case ModelType.Struct:
{
var value = Convert.ChangeType(row[name], pi.PropertyType);
pi.SetValue(model, value, null);
}
break;
case ModelType.Enum:
{
Type fiType = row[].GetType();
if (fiType == typeof(int))
{
pi.SetValue(model, row[name], null);
}
else if (fiType == typeof(string))
{
var value = (T)Enum.Parse(typeof(T), row[name].ToString());
if (value != null)
pi.SetValue(model, value, null);
}
}
break;
case ModelType.String:
{
var value = Convert.ChangeType(row[name], pi.PropertyType);
pi.SetValue(model, value, null);
}
break;
case ModelType.Object:
{
pi.SetValue(model, row[name], null);
}
break;
case ModelType.Else:
throw new Exception("不支持该类型转换");
default:
throw new Exception("未知类型");
}
}
}
#endregion
}
break;
default:
model = default(T);
break;
} return model;
}
}
}

后话,

1.可以通过缓存提高下性能。

每次typeof(T)后,将其对象相关信息(泛型属性等)存储起来,下次从缓存读取。

2.对SetValue改进。

可以使用泛型委托对其赋值。

3.用Emit

上一篇:XMLParser解析xml--内容源自网络(在静态库中不能用GDATA来解析,因为静态库不能加动态库)


下一篇:What is the difference between a binary tree, a binary search tree, a B tree and a B+ tree?