-1

我有这个object

Object
 Adress - count = 2 - System.Collections.Generic.List<Address>
  [0]   Address
    City - "Text" - string
    State - "Text" - string
  [1]   Address
    City - "Text" - string
    State - "Text" -string
 Notes - "Text" - string
 Items - Type(Item)
  Item - count = 2 - System.Collections.Generic.List<Item>
   [0]  Item
    Name - "Text" - string
    Price - "Text" - string
   [1]  Item
    Name - "Text" - string
    Price - "Text" - string

在这个对象中,Address 是所有地址的列表,notes 只是对象的另一个字段,items 有一个名为 item 的子项,它里面包含 Lists,就像地址一样。

我通过这段代码得到了笔记的价值:

foreach (var item in (object as IEnumerable).Cast<object>().ToList())
                    {
    String substring = item.GetType().GetProperty("Notes").GetValue(item).ToString();
}

这是我从该方法传递给 List 时得到的返回:System.Collections.Generic.List1[Address]

但我无法访问列表,我需要在 X 列表中分隔这个对象,如下所示:

Object2
 Adress
  [0]
  [1]

Object3
 Items
  Item
   [0]
   [1]

然后我可以使用上面的方法获取值,有什么建议吗?我已经尝试了很多方法...

4

1 回答 1

1

只需将 Adress 属性的值转换为List<Object>,就像这样

foreach (var item in (object as IEnumerable).Cast<object>().ToList())
{
    List<Object> address = item.GetType().GetProperty("Adress").GetValue(item) as List<Object>;
    foreach(var ad in address)
    {
        //do what you want here
    }
}
于 2017-12-12T13:45:58.553 回答