是指:Reflection - setting Type of returned obj? 我有一個對象調用Jobcard的幾個屬性,其中一個是另一個名爲Customer的對象,具有自己的屬性,其中一個是另一個名爲Adress的嵌套對象。反射 - 獲取嵌套對象的屬性
這兩個函數也將處理其他對象類型。
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;
}
的問題是,PopulateChildObject功能不起作用,因爲的PropertyInfo列表不在於傳遞childObj的。 如果我看看傳遞給手錶中的PopulateChildObject的dataObj,它有0個屬性。傳遞給PopChildObj()的dataObj也具有System.Reflection.RuntimePropertyInfo類型,而不是Customer類型。我錯過了什麼?
(回覆評論) – 2009-05-26 10:57:16