Say I have an C#.net DataTable Object, with all the data necessary to populate the below 3 object classes (Note that Foo contains a Bar and Cas)
I am not well versed in reflection, but I am working with a system that uses reflection to turn DataTable rows into ICollection's of Foo's, and it currently isn't sophisticated enough to populate the nested Bar and Cas objects in Foo without making additional database calls.
I trying to modify the existing function to make it recursive so it can can handle the potential future of Bar and Cas getting sub objects. Any help getting this to work would be appreciated. Moving away from refection would be a major overhaul and isn't going to be entertained by the team. Thanks in advance!
public static T Reflect<T>(DataTable dt, DataRow row, T item)
{
//all the T's on initial entry into this function will be Foo
foreach (var prop in item.GetType().GetProperties())
{
//if current prop is primitive, populate with value from row
//if current prop not primitive,
//recursively call this function with T of the type of Prop
if (!prop.PropertyType.IsPrimitive)
prop.SetValue(PropType, Reflect(dt, row, PropType));
}
}
Class Foo()
{
string Field1;
string Field2;
Bar Obj1;
Cas Obj2;
}
Class Bar()
{
string Field1;
string Field2;
}
Class Cas()
{
string Field1;
string Field2;
}
Aucun commentaire:
Enregistrer un commentaire