2009-05-26 103 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]); 
      }