Esta clase muestra como convertir arreglos de objetos en DataTables utilizando reflection.
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace F.Runtime
{
public class Converter
{
/// <summary>
/// Takes an object and put his fields values into an object array
/// </summary>
/// <param name="obj"></param>
/// <param name="T"></param>
/// <returns></returns>
public static object[] ToArray(object obj, DataTable T)
{
int i = 0;
object val;
object[] values = new object[T.Columns.Count];
foreach (FieldInfo f in obj.GetType().GetFields())
{
val = f.GetValue(obj);
values[i] = val;
i++;
}
return values;
}
/// <summary>
/// Build DataTable columns taking an object to discover it members /// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static DataTable BuildDataTable(object obj) {
DataTable T= new DataTable();
if (obj != null) {
foreach (PropertyInfo f in obj.GetType().GetProperties())
{
T.Columns.Add(f.Name, f.FieldType);
}
}
return T;
}
/// <summary>
/// Takes an object and add a new row to datatable
/// </summary>
/// <param name="obj"></param>
/// <param name="T"></param>
public static void AddRow(object obj, DataTable T)
{
T.Rows.Add(ToArray(obj,T));
}
/// <summary>
/// Takes an object and convert it to a DataRow
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static DataRow ToDataRow(object obj) {
DataRow Dr;
DataTable T = BuildDataTable(obj);
Dr = T.NewRow();
Dr.ItemArray = ToArray(obj, T);
return Dr;
}
/// <summary>
/// Takes a object collection and convert it into DataTable through reflection
/// </summary>
/// <param name="Collection"></param>
/// <returns></returns>
public static DataTable ToDataTable(object[] Collection)
{
DataTable T = null;
if (Collection != null)
{
if (Collection.Length > 0)
{
T = BuildDataTable(Collection[0]);
foreach (object obj in Collection)
{
AddRow(obj, T);
}
}
}
return T;
}
}
}
Transacciones Fiori
/UI2/CACHE Register service for UI2 cache use /UI2/CACHE_DEL Delete cache entries /UI2/CHIP Chip Registration /UI2/CUST Customizing of UI ...
-
Sobre todo en ambientes Industriales es muy socorrido el uso de impresoras Zebra, en seguida el código fuente, la siguiente clase implement...
-
Esta característica de ORACLE es muy útil para resolver múltiples necesidades, por ejemplo cuando un mismo algoritmo se aplica a diferentes ...