using System;
using System.Data;
using System.Reflection;
using System.Reflection.Emit;
namespace ObjToAdo
{
/// <summary>
/// Summary description for Converter.
/// </summary>
public class Converter
{
private Converter() {}
/// <summary>
///
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static DataTable ConvertToDataTable(Object o)
{
PropertyInfo[] properties = o.GetType().GetProperties();
DataTable dt = CreateDataTable(properties);
FillData(properties, dt, o);
return dt;
}
/// <summary>
///
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static DataTable ConvertToDataTable(Object[] array)
{
PropertyInfo[] properties = array.GetType().GetElementType().GetProperties();
DataTable dt = CreateDataTable(properties);
if (array.Length != 0)
{
foreach(object o in array)
FillData(properties, dt, o);
}
return dt;
}
/// <summary>
///
/// </summary>
/// <param name="properties"></param>
/// <returns></returns>
private static DataTable CreateDataTable(PropertyInfo[] properties)
{
DataTable dt = new DataTable();
DataColumn dc = null;
foreach(PropertyInfo pi in properties)
{
dc = new DataColumn();
dc.ColumnName = pi.Name;
dc.DataType = pi.PropertyType;
dt.Columns.Add(dc);
}
return dt;
}
/// <summary>
///
/// </summary>
/// <param name="properties"></param>
/// <param name="dt"></param>
/// <param name="o"></param>
private static void FillData(PropertyInfo[] properties, DataTable dt, Object o)
{
DataRow dr = dt.NewRow();
foreach(PropertyInfo pi in properties)
dr[pi.Name] = pi.GetValue(o, null);
dt.Rows.Add(dr);
}
}
}
herve leger, timberland boots, giuseppe zanotti, iphone cases, insanity workout, mac cosmetics, reebok shoes, hollister, bottega veneta, vans shoes, soccer shoes, gucci, mont blanc, new balance, soccer jerseys, nike roshe, babyliss, converse, valentino shoes, ray ban, jimmy choo shoes, birkin bag, north face outlet, nike air max, baseball bats, nike air max, hollister, louboutin, nike trainers, instyler, ralph lauren, chi flat iron, hollister, ferragamo shoes, vans, nfl jerseys, converse outlet, mcm handbags, nike huarache, celine handbags, p90x workout, wedding dresses, abercrombie and fitch, north face outlet, longchamp, lululemon, ghd, oakley, asics running shoes, beats by dre
ReplyDelete