2016-05-12 10 views
1

次のコードがあります。リフレクションによってネストされたプロパティ値を取得するC#

クラス:

public class AlloyDock 
{ 
    public int Left { get; set; } 
    public int Right { get; set; } 
} 

public class Charger 
{ 
    public int Left { get; set; } 
    public int Right { get; set; } 
} 
public class VehicleControlTest 
{ 
    public Charger Charger1 { get; set; } 
} 
public class BasicControlTest 
{ 
    public AlloyDock AlloyDock1 { get; set; } 
} 
class Appointment 
{ 
    public BasicControlTest BasicControlTest1 { get; set; } 
    public VehicleControlTest VehicleControlTest1 { get; set; } 
} 

主な機能:

 var obj = new Appointment(); 
     obj.BasicControlTest1 = new BasicControlTest(); 
     obj.BasicControlTest1.AlloyDock1 = new AlloyDock(); 
     obj.BasicControlTest1.AlloyDock1.Left = 1; 
     obj.BasicControlTest1.AlloyDock1.Right = 2; 

     obj.VehicleControlTest1 = new VehicleControlTest(); 
     obj.VehicleControlTest1.Charger1 = new Charger(); 
     obj.VehicleControlTest1.Charger1.Left = 3; 
     obj.VehicleControlTest1.Charger1.Right = 4; 


     var parentProperties = obj.GetType().GetProperties(); 
     foreach (var prop in parentProperties) 
     { 
      // Get Main objects inside each test type. 
      var mainObjectsProperties = prop.PropertyType.GetProperties(); 
      foreach (var property in mainObjectsProperties) 
      { 
       var leafProperties = property.PropertyType.GetProperties(); 
       foreach (var leafProperty in leafProperties) 
       { 
        Console.WriteLine("{0}={1}", leafProperty.Name, leafProperty.GetValue(obj, null)); 
       } 
      } 
     } 

は、私は、プロパティ名とリーフノードの値を取得したいです。私は名前を得ることができますが、私が価値を得ようとすると(それぞれ1,2,3,4)。私は以下のエラーに陥っています。

オブジェクトがターゲットタイプと一致しません。

私はこの問題を解決するために頭を叩いています。 誰でも助けてくれますか?

+0

親オブジェクトインスタンスをリーフプロパティ情報ゲッターに渡しています。 – Groo

+0

どうすれば解決できますか? –

+0

なぜ反射が必要ですか? –

答えて

1

GetValueメソッドにオブジェクトのインスタンスを渡すとき、あなたは正しい型のインスタンスを渡す必要があります。

// 1st level properties 
var parentProperties = obj.GetType().GetProperties(); 
foreach (var prop in parentProperties) 
{ 
    // get the actual instance of this property 
    var propertyInstance = prop.GetValue(obj, null); 

    // get 2nd level properties 
    var mainObjectsProperties = prop.PropertyType.GetProperties(); 

    foreach (var property in mainObjectsProperties) 
    { 
     // get the actual instance of this 2nd level property 
     var leafInstance = property.GetValue(propertyInstance, null); 

     // 3rd level props 
     var leafProperties = property.PropertyType.GetProperties(); 

     foreach (var leafProperty in leafProperties) 
     { 
      Console.WriteLine("{0}={1}", 
       leafProperty.Name, leafProperty.GetValue(leafInstance, null)); 
     } 
    } 
} 

あなたは(一般)単純化するために再帰的にこれを行う可能性があります全体のこと:

static void DumpObjectTree(object propValue, int level = 0) 
{ 
    if (propValue == null) 
     return; 

    var childProps = propValue.GetType().GetProperties(); 
    foreach (var prop in childProps) 
    { 
     var name = prop.Name; 
     var value = prop.GetValue(propValue, null); 

     // add some left padding to make it look like a tree 
     Console.WriteLine("".PadLeft(level * 4, ' ') + "{0}={1}", name, value); 

     // call again for the child property 
     DumpObjectTree(value, level + 1); 
    } 
} 

// usage: DumpObjectTree(obj); 
+0

ありがとうございます。チャームのように働いた:) –

+0

@Beelal:ツリーの深さをハードコードにしたくない場合は、以下の再帰的メソッドのようなものを使うことをお勧めします。 – Groo

+0

私はそれを見ています –

0

問題は、この式である:

leafProperty.GetValue(obj, null) 

あなたは葉のプロパティを取得するには、ルート・オブジェクトを渡しています。各プロパティを処理する際には、その値を取得し、GetValueを呼び出す必要があります。

+0

私を導いてくれますか? "どうやって"?私はReflection APIの新人です –

+0

@BeelalAhmed - Grooの答えを見てください。 – Sean

関連する問題