代码之家  ›  专栏  ›  技术社区  ›  callisto

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

  •  2
  • callisto  · 技术社区  · 17 年前

    指: Reflection - setting Type of returned obj?

    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;
    }
    

    2 回复  |  直到 9 年前
        1
  •  3
  •   Marc Gravell    17 年前

    propertyitem PropertyInfo 从财产-即。

    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);
    

    PopulateObject PopulateChildObject 感觉它们可能是同一件事?

        2
  •  0
  •   Mohamed.Abdo    12 年前

    获取Nest属性,例如Developer。项目。名字

    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]);
                }
    
    推荐文章