CS中的实用程序(-)Ilist 转换到datatable
datatable在某些方面要比Ilist操作起来方便很多,尤其是修改一些老程序时,客户端已经都绑定到了Dataset或者datatable,如果让他们重新绑定到Ilist会做大量的工作。
如果能转换到Datatable会方便很多
public class CADataConverter
{
private static Hashtable types = new Hashtable();
private CADataConverter()
{
}
/// <summary>
/// Converts an IList into a datatable.
/// </summary>
/// <param name="list">List of objects. First item can not be null</param>
/// <returns></returns>
public static DataTable ToDataTable(IList list)
{
if(list == null)
throw new ArgumentNullException("list", "List can not be null");
if(list.Count == 0)
throw new ArgumentOutOfRangeException("list", "List can not be empty without supplying a type. Please use the other overload");
object obj = list[0];
if(obj == null)
throw new ArgumentOutOfRangeException("list", "First item in collection can not equal null");
return ToDataTable(list,obj.GetType());
}
/// <summary>
/// Converts an IList into a datatable.
/// </summary>
/// <param name="list"></param>
/// <param name="t">Since a type is supplied, the list can be null or empty</param>
/// <returns></returns>
public static DataTable ToDataTable(IList list, Type t)
{
DataTable dt = types[t] as DataTable;
if(dt == null)
dt = CreateShell(t);
else
dt = dt.Clone();
if(list == null || list.Count == 0)
return dt;
foreach(object item in list)
{
if(item != null)
{
DataRow dr = dt.NewRow();
foreach(DataColumn dc in dt.Columns)
{
try
{
dr[dc.ColumnName] = t.GetProperty(dc.ColumnName).GetValue(item,null);
}
catch{}
}
dt.Rows.Add(dr);
}
}
return dt;
}
/// <summary>
/// Creates a datatable with datacolumns
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
protected static DataTable CreateShell(Type t)
{
DataTable dt = new DataTable(t.Name);
PropertyInfo[] pia = t.GetProperties();
foreach (PropertyInfo pi in pia)
{
if(pi.CanRead)
{
string st = pi.PropertyType.ToString();
switch(st)
{
case "System.String":
case "System.Int32":
case "System.Boolean":
case "System.Double":
case "System.Guid":
dt.Columns.Add(pi.Name,pi.PropertyType);
break;
}
}
}
types[t] = dt;
return dt;
}
}
{
private static Hashtable types = new Hashtable();
private CADataConverter()
{
}
/// <summary>
/// Converts an IList into a datatable.
/// </summary>
/// <param name="list">List of objects. First item can not be null</param>
/// <returns></returns>
public static DataTable ToDataTable(IList list)
{
if(list == null)
throw new ArgumentNullException("list", "List can not be null");
if(list.Count == 0)
throw new ArgumentOutOfRangeException("list", "List can not be empty without supplying a type. Please use the other overload");
object obj = list[0];
if(obj == null)
throw new ArgumentOutOfRangeException("list", "First item in collection can not equal null");
return ToDataTable(list,obj.GetType());
}
/// <summary>
/// Converts an IList into a datatable.
/// </summary>
/// <param name="list"></param>
/// <param name="t">Since a type is supplied, the list can be null or empty</param>
/// <returns></returns>
public static DataTable ToDataTable(IList list, Type t)
{
DataTable dt = types[t] as DataTable;
if(dt == null)
dt = CreateShell(t);
else
dt = dt.Clone();
if(list == null || list.Count == 0)
return dt;
foreach(object item in list)
{
if(item != null)
{
DataRow dr = dt.NewRow();
foreach(DataColumn dc in dt.Columns)
{
try
{
dr[dc.ColumnName] = t.GetProperty(dc.ColumnName).GetValue(item,null);
}
catch{}
}
dt.Rows.Add(dr);
}
}
return dt;
}
/// <summary>
/// Creates a datatable with datacolumns
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
protected static DataTable CreateShell(Type t)
{
DataTable dt = new DataTable(t.Name);
PropertyInfo[] pia = t.GetProperties();
foreach (PropertyInfo pi in pia)
{
if(pi.CanRead)
{
string st = pi.PropertyType.ToString();
switch(st)
{
case "System.String":
case "System.Int32":
case "System.Boolean":
case "System.Double":
case "System.Guid":
dt.Columns.Add(pi.Name,pi.PropertyType);
break;
}
}
}
types[t] = dt;
return dt;
}
}
通过反射取到Type的信息,type的属性被做为列名,CreatShell 实现
类型的属性值作为行。TODatatable实现
调用:
DataTable dt = CADataConverter.ToDataTable(categories,typeof(PostCategory));