反射 - 获取嵌套对象的属性

时间:2009-05-26 09:24:14

标签: c# reflection propertyinfo getproperties

指的是: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的列表。 如果我查看在watch中传递给PopulateChildObject的dataObj,它有0个Attributes。传递给PopChildObj()的dataObj也有类型System.Reflection.RuntimePropertyInfo'而不是Customer类型。我错过了什么?

2 个答案:

答案 0 :(得分:3)

propertyitemPropertyInfo;你需要从属性中传递 - 即

propertyItem.GetValue(dataObj, null);

如果父对象创建了这个子对象,则不需要“设置”它;只需更新底层对象:

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之间真的有什么不同吗?感觉它们可能是同一个东西?

答案 1 :(得分:0)

获取Nest属性,例如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]);
            }
相关问题