2009-05-26 90 views
2

是指: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類型。我錯過了什麼?

+0

(回覆評論) – 2009-05-26 10:57:16

回答

3

propertyitemPropertyInfo;你需要從屬性傳遞給它 - 即

propertyItem.GetValue(dataObj, null); 

如果此子對象是由父母創建的,你不應該需要「設置」它;只需更新underyling對象:

PopulateChildObject(propertyitem.GetValue(dataObj, null), dataRow); 

它可能是你需要創建子對象(通常Activator.CreateInstance),在這種情況下,你需要調用SetValue

object child = propertyitem.GetValue(dataObj, null); 
if(child == null) { 
    child = Activator.CreateInstance(propertyitem.PropertyType); 
    propertyitem.SetValue(dataObj, child, null); 
} 
PopulateChildObject(child, dataRow); 

我不知道 - PopulateObjectPopulateChildObject之間真的有區別嗎?感覺他們可能是同一件事?

+0

PopulateObject和PopulateChildObject之間的區別: 我不能得到PopulateObject遞歸調用工作,因爲childobject的類型有也可以通過。 – callisto 2009-05-26 10:30:41

0

獲取巢性質如Developer.Project.Name

private static System.Reflection.PropertyInfo GetProperty(object t, string PropertName) 
      { 
       if (t.GetType().GetProperties().FirstOrDefault(p => p.Name == PropertName.Split('.')[0]) == null) 
        throw new ArgumentNullException(string.Format("Property {0}, is not exists in object {1}", PropertName, t.ToString())); 
       if (PropertName.Split('.').Length == 1) 
        return t.GetType().GetProperty(PropertName); 
       else 
        return GetProperty(t.GetType().GetProperty(PropertName.Split('.')[0]).GetValue(t, null), PropertName.Split('.')[1]); 
      }