【发布时间】:2018-05-26 04:45:00
【问题描述】:
我有这个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]
然后我可以使用上述方法获取值,有什么建议吗?我已经尝试了很多方法...
【问题讨论】:
-
如果你能创建一个minimal reproducible example,那就太棒了。
-
我需要从对象内部的列表中获取值,我无法获取,我只是从注释中获取值(那不是列表)
-
我的
foreach只是将注释值返回给我,当它通过地址时,我返回:System.Collections.Generic.List1[Address] -
我认为至少您只需将您的 Object 的来源提供给我们,我们就可以帮助您。
-
我的对象是在程序的另一部分创建的,它具有我在问题上写的那种结构,我怎样才能更好地解释? (我是堆栈新手)
标签: c# list object ienumerable getproperty