refers to : http://stackoverflow.com/questions/906327/reflection-setting-type-of-returned-obj I have a object Call Jobcard with a few properties, one of which is another object called Customer with its own properties, one of which is another nested object called Adress.
These 2 functions will be handling other object types as well.
private T PopulateObject<T>(T dataObj, System.Data.DataRow dataRow)
{
//Type type = dataObj.GetType();
System.Reflection.PropertyInfo[] proplist = dataObj.GetType().GetProperties();
foreach ( System.Reflection.PropertyInfo propertyitem in proplist)
{
if(propertyitem.Name != "")
//s += propertyitem.Name + ":" + (propertyitem.GetValue(dataObj,null)).ToString() + "\r\n";
try
{
propertyitem.SetValue(dataObj, dataRow[propertyitem.Name], null);
}
catch (Exception ex)
{
if (ex.Message.Contains("does not belong to table"))
{
propertyitem.SetValue(dataObj, PopulateChildObject(propertyitem, dataRow), null);
}
else
throw;
}
}
return dataObj;
}
private object PopulateChildObject(object dataObj, System.Data.DataRow dataRow)
{
System.Reflection.PropertyInfo[] proplist = dataObj.GetType().GetProperties();
foreach ( System.Reflection.PropertyInfo propertyitem in proplist)
{
if(propertyitem.Name != "")
try
{
propertyitem.SetValue(dataObj, dataRow[propertyitem.Name], null);
}
catch (Exception ex)
{
if (ex.Message.Contains("does not belong to table"))
{
propertyitem.SetValue(dataObj, PopulateChildObject(propertyitem, dataRow), null);
}
else
throw;
}
}
return dataObj;
}
The problem is that the PopulateChildObject function does not work because the PropertyInfo list is not that of the passed childObj. If I look at the dataObj passed to PopulateChildObject in the watch, it has 0 Attributes. Also the dataObj passed to PopChildObj() has type of System.Reflection.RuntimePropertyInfo' instead of type Customer. What am I missing?