using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using System.Text.RegularExpressions;
using DataAccess;
using System.Diagnostics;
using System.Reflection;
namespace Business
{
/// <summary>
/// 实体类转换
/// </summary>
/// <typeparam name="T"></typeparam>
public class CModelTableFill<T> where T : new()
{
/// <summary>
/// 填充多个实体类对象
/// </summary>
/// <param name="dt">数据集</param>
/// <returns>实体类</returns>
public List<T> FillModel(DataTable dt)
{
if (dt == null || dt.Rows.Count == 0)
{
return null;
}
T t = new T();
List<T> modelList = new List<T>();
PropertyInfo[] arrayInfo = t.GetType().GetProperties();
foreach (DataRow itemRow in dt.Rows)
{
T model = new T();
foreach (DataColumn itemColumn in dt.Columns)
{
foreach (PropertyInfo info in arrayInfo)
{
if (info.Name.ToUpper() == itemColumn.ColumnName)
{
if (itemRow[itemColumn.ColumnName] != DBNull.Value)
{
info.SetValue(model, itemRow[itemColumn.ColumnName].ToString(), null);
}
}
}
}
modelList.Add(model);
}
return modelList;
}
/// <summary>
/// 填充一个实体类对象
/// </summary>
/// <param name="dt">数据集</param>
/// <returns>实体类</returns>
public T FillModelRow(DataTable dt)
{
if (dt == null || dt.Rows.Count == 0)
{
return default(T);
}
T model = new T();
PropertyInfo[] arrayInfo = model.GetType().GetProperties();
for (int i = 0; i < dt.Columns.Count; i++)
{
foreach (PropertyInfo info in arrayInfo)
{
if (info.Name.ToUpper() == dt.Columns[i].ColumnName)
{
if (dt.Rows[0][i] != DBNull.Value)
{
info.SetValue(model, dt.Rows[0][i].ToString(), null);
break;
}
}
}
}
return model;
}
}
}