【问题标题】:Reflection - getting properties of nested objects反射 - 获取嵌套对象的属性
【发布时间】:2009-05-26 09:24:14
【问题描述】:

指的是:Reflection - setting Type of returned obj? 我有一个对象 Call 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 类型。我错过了什么?

【问题讨论】:

    标签: c# reflection propertyinfo getproperties


    【解决方案1】:

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

    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 之间真的有什么区别吗?感觉它们可能是同一回事?

    【讨论】:

    • PopulateObject 和 PopulateChildObject 之间的区别:我无法对 PopulateObject 进行递归调用,因为还必须传递子对象的类型。
    • 只是因为你把它变成了通用的——但是(根据我对上一个问题的回答)这里的泛型没有什么好处。
    • 谢谢马克。在您的另一篇文章的帮助下,我得到了它的帮助:eggheadcafe.com/…propertyitem.PropertyType;是我需要做的
    【解决方案2】:

    获取 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]);
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多