K
Kevin Schneider
I have the "simple" problem of wanting to take an array of objects I
get from a webservice and turning them into a DataTable. The following
is my solution and I'm looking for a critique of what I am doing. The
names in the code below have been changed to "protect the company",
but the spirit remains. I've posted it as if you would call
getAndFillDT(). Thanks
using System;
using System.Data;
using System.Reflection;
namespace Testing
{
public class DTfromWS
{
public DTfromWS()
{
}
private DataTable getAndFillDT ()
{
MyWebService.VO vo = new MyWebService.VO();
Array mvo = taiws.findAllVOs(vo);
DataTable dt = makeDataTable(new MyWebService.VO());
foreach (MyWebServiceVO voi in mvo)
{
System.Type mt= voi.GetType();
System.Reflection.FieldInfo[] fia =
mt.GetFields(BindingFlags.Instance
| BindingFlags.Public
| BindingFlags.DeclaredOnly);
DataRow dr = dt.NewRow();
foreach (System.Reflection.FieldInfo fi in fia)
{
try
{
dr[fi.Name] = mt.InvokeMember(
fi.Name,
System.Reflection.BindingFlags.GetField
| BindingFlags.Instance
| BindingFlags.Public
| System.Reflection.BindingFlags.DeclaredOnly,
null,
voi,
null);
}
catch (Exception ex)
{
throw new Exception(fi.Name + " current field name", ex);
}
}
dt.Rows.Add(dr);
}
}
public DataTable makeDataTable(object VO)
{
DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
System.Type mt= VO.GetType();
System.Reflection.FieldInfo[] fia =
mt.GetFields(BindingFlags.Instance
| BindingFlags.Public
| BindingFlags.DeclaredOnly);
foreach (System.Reflection.FieldInfo fi in fia)
{
dt.Columns.Add(fi.Name,fi.FieldType);
}
return dt;
}
}
}
get from a webservice and turning them into a DataTable. The following
is my solution and I'm looking for a critique of what I am doing. The
names in the code below have been changed to "protect the company",
but the spirit remains. I've posted it as if you would call
getAndFillDT(). Thanks
using System;
using System.Data;
using System.Reflection;
namespace Testing
{
public class DTfromWS
{
public DTfromWS()
{
}
private DataTable getAndFillDT ()
{
MyWebService.VO vo = new MyWebService.VO();
Array mvo = taiws.findAllVOs(vo);
DataTable dt = makeDataTable(new MyWebService.VO());
foreach (MyWebServiceVO voi in mvo)
{
System.Type mt= voi.GetType();
System.Reflection.FieldInfo[] fia =
mt.GetFields(BindingFlags.Instance
| BindingFlags.Public
| BindingFlags.DeclaredOnly);
DataRow dr = dt.NewRow();
foreach (System.Reflection.FieldInfo fi in fia)
{
try
{
dr[fi.Name] = mt.InvokeMember(
fi.Name,
System.Reflection.BindingFlags.GetField
| BindingFlags.Instance
| BindingFlags.Public
| System.Reflection.BindingFlags.DeclaredOnly,
null,
voi,
null);
}
catch (Exception ex)
{
throw new Exception(fi.Name + " current field name", ex);
}
}
dt.Rows.Add(dr);
}
}
public DataTable makeDataTable(object VO)
{
DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
System.Type mt= VO.GetType();
System.Reflection.FieldInfo[] fia =
mt.GetFields(BindingFlags.Instance
| BindingFlags.Public
| BindingFlags.DeclaredOnly);
foreach (System.Reflection.FieldInfo fi in fia)
{
dt.Columns.Add(fi.Name,fi.FieldType);
}
return dt;
}
}
}