using System;
using System.Data;
using System.Reflection;
namespace Masuit.Tools.Database
{
///
/// SqlDataReader扩展类
///
public static class DataExt
{
///
/// 根据DataRow映射到实体模型
///
/// 实体模型
/// 数据行
/// 映射后的实体模型
public static T MapEntity(this DataRow row) where T : class
{
T obj = Assembly.GetAssembly(typeof(T)).CreateInstance(typeof(T).FullName) as T;
Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo p in properties)
{
p.SetValue(obj, row[p.Name]);
}
return obj;
}
///
/// 根据DataReader映射到实体模型
///
/// 实体模型
/// IDataReader
/// 映射后的实体模型
public static T MapEntity(this IDataReader dr) where T : class
{
T obj = Assembly.GetAssembly(typeof(T)).CreateInstance(typeof(T).FullName) as T;
Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo p in properties)
{
p.SetValue(obj, dr[p.Name]);
}
return obj;
}
}
}