2016-05-12 70 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