using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using SCC.Common;
namespace SCC.Services
{
///
/// 基础服务
///
public class BaseServices
{
///
/// 装箱单个数据对象
///
/// 装箱对象
/// 装箱数据行
///
protected T LoadData(DataRow dr)
{
if (dr == null) return default(T);
var t = typeof(T);
var obj = Activator.CreateInstance(t);
var properts = t.GetProperties();
foreach (var pi in properts)
{
if (!dr.Table.Columns.Contains(pi.Name)) continue;
pi.SetValue(obj, CommonHelper.ChangeType(dr[pi.Name], pi.PropertyType), null);
}
return (T)obj;
}
///
/// 装箱列表数据对象
///
/// 装箱对象
/// 装箱数据来源表格
///
protected List LoadDataList(DataTable dt)
{
List result = new List();
var t = typeof(T);
var properts = t.GetProperties();
object obj;
foreach (DataRow dr in dt.Rows)
{
obj = Activator.CreateInstance(t);
foreach (var pi in properts)
{
if (!dt.Columns.Contains(pi.Name)) continue;
pi.SetValue(obj, CommonHelper.ChangeType(dr[pi.Name], pi.PropertyType), null);
}
result.Add((T)obj);
}
return result;
}
}
}