【转】LmgORM项目 实体类转换器
本文来自:http://www.cnblogs.com/luomingui/archive/2011/04/16/2018480.html
LmgORM项目 实体类转换器
主要包含以下转换 :
- 使用DbDataReader填充对象.
- 使用DataSet填充对象.
- dataset转实体类
- 将实体类转换成DataTable
- 转换IList<T>为List<T>
- List<T>转换为ArrayList
///<summary>
/// 实体类转换器
///</summary>
publicclass TypeConverter
{
///<summary>
/// 使用SQL填充对象.
///</summary>
///<typeparam name="T"></typeparam>
///<param name="fieldItem"></param>
///<param name="reader"></param>
///<returns></returns>
publicstatic ArrayList FillModelForSql<T>(DataItem fieldItem,
System.Data.Common.DbDataReader reader) where T : new()
{
ArrayList resultList =new ArrayList();
Hashtable hashTable = fieldItem.DataValueToHashTable();
while (reader.Read())
{
T emptyDataObject =new T();
for (int i =0; i < reader.FieldCount; i++)
{
Object obj = reader.GetValue(i);
if (hashTable[reader.GetName(i).ToString().ToUpper()] !=null)
{
CommonHelper.Instance.SetProValue<T>(emptyDataObject,
hashTable[reader.GetName(i).
ToString().ToUpper()].ToString(), obj);
}
}
resultList.Add(emptyDataObject);
}
return resultList;
}
///<summary>
/// 使用SQL填充对象.
///</summary>
///<typeparam name="T"></typeparam>
///<param name="fieldItem"></param>
///<param name="reader"></param>
///<returns></returns>
publicstatic ArrayList FillForSql<T>(DataItem fieldItem,
DataSet dataset) where T : new()
{
ArrayList resultList =new ArrayList();
Hashtable hashTable = fieldItem.DataValueToHashTable();
foreach (DataRow dr in dataset.Tables[0].Rows)
{
T emptyDataObject =new T();
foreach (DataColumn dc in dr.Table.Columns)
{
if (hashTable[dc.ColumnName.ToUpper()] !=null)
{
CommonHelper.Instance.SetProValue<T>(emptyDataObject,
hashTable[dc.ColumnName.ToUpper()].ToString(), dr[dc.ColumnName]);
}
}
resultList.Add(emptyDataObject);
}
return resultList;
}
///<summary>
/// 使用SQL填充对象.
///</summary>
///<param name="typeEntity"></param>
///<param name="fieldItem"></param>
///<param name="reader"></param>
///<returns></returns>
publicstatic ArrayList QueryEntityListForSql(Type typeEntity,
DataItem fieldItem, System.Data.Common.DbDataReader reader)
{
ArrayList resultList =new ArrayList();
Hashtable hashTable = fieldItem.DataValueToHashTable();
while (reader.Read())
{
object emptyDataObject = Activator.CreateInstance(typeEntity);
for (int i =0; i < reader.FieldCount; i++)
{
Object obj = reader.GetValue(i);
if (hashTable[reader.GetName(i).ToString().ToUpper()] !=null)
{
CommonHelper.Instance.SetProValue(typeEntity, emptyDataObject,
hashTable[reader.GetName(i).ToString().
ToUpper()].ToString(), obj);
}
}
resultList.Add(emptyDataObject);
}
return resultList;
}
///<summary>
/// dataset转实体类
///</summary>
///<typeparam name="T">实体类</typeparam>
///<param name="ds">dataset</param>
///<returns></returns>
publicstatic IList<T> FillModel<T>(DataSet ds)
{
List<T> l =new List<T>();
T model =default(T);
if (ds.Tables[0].Columns[0].ColumnName =="rowId"){
ds.Tables[0].Columns.Remove("rowId");
}
foreach (DataRow dr in ds.Tables[0].Rows)
{
model = Activator.CreateInstance<T>();
foreach (DataColumn dc in dr.Table.Columns)
{
try
{
PropertyInfo pi = model.GetType().GetProperty(dc.ColumnName);
if (dr[dc.ColumnName] != DBNull.Value)
pi.SetValue(model, dr[dc.ColumnName], null);
else
pi.SetValue(model, null, null);
}
catch {
continue;
}
}
l.Add(model);
}
return l;
}
///<summary>
/// 将实体类转换成DataTable
///</summary>
///<typeparam name="T"></typeparam>
///<param name="i_objlist"></param>
///<returns></returns>
publicstatic DataTable Fill<T>(IList<T> objlist)
{
if (objlist ==null|| objlist.Count <=0)
{
returnnull;
}
DataTable dt =new DataTable(typeof(T).Name);
DataColumn column;
DataRow row;
System.Reflection.PropertyInfo[] myPropertyInfo =
typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (T t in objlist)
{
if (t ==null)
{
continue;
}
row = dt.NewRow();
for (int i =0, j = myPropertyInfo.Length; i < j; i++)
{
System.Reflection.PropertyInfo pi = myPropertyInfo[i];
string name = pi.Name;
if (dt.Columns[name] ==null)
{
column =new DataColumn(name, pi.PropertyType);
dt.Columns.Add(column);
}
row[name] = pi.GetValue(t, null);
}
dt.Rows.Add(row);
}
return dt;
}
///<summary>
/// 转换IList<T>为List<T>
///</summary>
///<typeparam name="T">指定的集合中泛型的类型</typeparam>
///<param name="gbList">需要转换的IList</param>
///<returns></returns>
publicstatic List<T> ConvertIListToList<T>(IList gbList) where T : class
{
if (gbList !=null&& gbList.Count >1)
{
List<T> list =new List<T>();
for (int i =0; i < gbList.Count; i++)
{
T temp = gbList[i] as T;
if (temp !=null)
list.Add(temp);
}
return list;
}
returnnull;
}
///<summary>
/// List<T>转换为ArrayList
///</summary>
///<typeparam name="T"></typeparam>
///<param name="list"></param>
///<returns></returns>
publicstatic ArrayList GenericToArrayList<T>(List<T> list)
{
ArrayList alist =new ArrayList();
foreach (T t in list)
{
alist.Add(t);
}
return alist;
}
}