码迷,mamicode.com
首页 > 其他好文 > 详细

ListHelper

时间:2014-07-07 23:51:29      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:cti   for   io   new   type   amp   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace Data_Helper
{
/// <summary>
/// List 操作(dataTable、dataSet 等之间的转换)
/// </summary>
public class ListHelper
{
/// <summary>
/// 将 dataTable 转换成 List
/// </summary>
/// <typeparam name="T">要转换成的对象</typeparam>
/// <param name="dataTable">要转换的DataTable</param>
/// <returns></returns>
public static List<T> ConvertToList<T>(DataTable dataTable)
{
T obj = default(T);
List<T> objList = new List<T>();
PropertyInfo[] objProperties = null;
string propertieName = null;

try
{
foreach (DataRow row in dataTable.Rows)
{
obj = Activator.CreateInstance<T>();//动态创建类型实例,不需要引用类库
objProperties = obj.GetType().GetProperties();
foreach (PropertyInfo properties in objProperties)
{
if (properties != null)
propertieName = properties.Name;

if (propertieName != null && dataTable.Columns.Contains(propertieName))
{
object value = row[propertieName];
if (value.GetType() == typeof(System.DBNull))
value = null;

properties.SetValue(obj, value, null);
}
}

objList.Add(obj);
}
}
catch (Exception ex)
{
throw ex;
}

return objList;
}

/// <summary>
/// 将 List 转换成 dataTable
/// </summary>
/// <typeparam name="T">要转换成的对象</typeparam>
/// <param name="objList">要转换的List</param>
/// <returns></returns>
public static DataTable ConvertToDataTable<T>(List<T> objList)
{
if (objList == null || objList.Count <= 0)
return null;

DataTable dt = new DataTable(typeof(T).Name);
PropertyInfo[] objProperties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

try
{
DataColumn column;
DataRow row;
PropertyInfo property;
string propertyName = null;

foreach (T obj in objList)
{
if (obj == null)
continue;

row = dt.NewRow();
for (int i = 0, j = objProperties.Length; i < j; i++)
{
property = objProperties[i];
propertyName = property.Name;

if (propertyName != null && dt.Columns[propertyName] == null)
{
column = new DataColumn(propertyName, property.PropertyType);
dt.Columns.Add(column);
}

row[propertyName] = property.GetValue(obj, null);
}

dt.Rows.Add(row);
}
}
catch (Exception ex)
{
throw ex;
}

return dt;
}

/// <summary>
/// 将 List 转换成 dataSet
/// </summary>
/// <typeparam name="T">要转换成的对象</typeparam>
/// <param name="objList">要转换的objList</param>
/// <returns></returns>
public static DataSet ConvertToDataSet<T>(List<T> objList)
{
if (objList == null || objList.Count <= 0)
return null;

DataSet ds = new DataSet();

try
{
DataTable dt = ConvertToDataTable(objList);
ds.Tables.Add(dt);
}
catch (Exception ex)
{
throw ex;
}

return ds;
}

}
}

ListHelper,布布扣,bubuko.com

ListHelper

标签:cti   for   io   new   type   amp   

原文地址:http://www.cnblogs.com/liuyudun/p/3812720.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!