2009-05-26 33 views
2

参照先:Reflection - setting Type of returned obj? いくつかのプロパティを持つオブジェクト・コール・カードがあります。その1つは、独自のプロパティを持つCustomerという別のオブジェクトです。その1つはAdressという別のネストされたオブジェクトです。入れ子オブジェクトのリフレクション取得プロパティ

これらの2つの関数は、他のオブジェクト型も処理します。

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

問題はPropertyInfoリストが渡されchildObjのものではありませんので、PopulateChildObject機能が動作しないということです。 Watch内のPopulateChildObjectに渡されたdataObjを見ると、属性は0です。また、PopChildObj()に渡されるdataObjには、Customer型の代わりにSystem.Reflection.RuntimePropertyInfoの型があります。私は何が欠けていますか?

+0

(コメントに返信) –

答えて

3

propertyitemは、PropertyInfoです。あなたはプロパティからそれをを渡す必要がある - すなわち

propertyItem.GetValue(dataObj, null); 

この子オブジェクトが親に​​よって作成された場合、あなたはそれを「設定」する必要はありません。ちょうどunderylingオブジェクトを更新:

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

それはあなたがに必要なことがありますSetValueを呼び出すために必要があります。その場合には、子オブジェクト(通常はActivator.CreateInstance)を作成します。

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の違い:childobjectの種類がに持っているので、私は働いてPopulateObjectへの再帰呼び出しを取得できませんでした また、渡されます。 – callisto

+0

あなたはそれを一般的なものにしたからです - しかし、(前の質問に対する私の返答によると)ここではジェネリックスにはほとんど効果がありません。 –

+0

ありがとうございました。私はあなたから別の投稿の助けを借りてそれをwotkingしました:http://www.eggheadcafe.com/conversation.aspx?messageid=30043135&threadid=30043131 propertyitem.PropertyType; は私がする必要があります – callisto

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]); 
      } 
関連する問題