dynamic model = new ExpandoObject();
model.Data = "asdf";
List<dynamic> listOfx = new List<dynamic>();
for (int i = 0; i < 3; i++) {
dynamic x = new ExpandoObject();
x.ID = i;
x.Name = "test" + i.ToString();
listOfx.Add(x);
}
model.listOfx = listOfx;
当我运行它时,我可以看到模型中的数据,但看不到 listOfx。
问题:如何在 ExpandoObject 中获取列表(或 IEnumerable)
解决方案更新:
因为我在本地窗口中看不到 lifOfx,所以我认为它没有工作。在这里(通过 y)你可以看到它是。 :-)
最佳答案
我无法在 Mono 2.10 上重现类似问题:
using System.Dynamic;
using System.Collections.Generic;
using System;
public class Program
{
public static void Main(string[] args)
{
dynamic x = new ExpandoObject();
x.Data ="test";
x.Arr = new [] { "test1","test2"};
x.Lst = new List<string> { "aap", "noot", "mies" };
Console.WriteLine(string.Join(", ", x.Arr));
Console.WriteLine(string.Join(", ", x.Lst));
}
}
输出:
/tmp @ dmcs test.cs && mono test.exe
test1, test2
aap, noot, mies
我很快就会在 Windows 上重新测试。
更新测试了以下内容:
- Linux 编译的 (dmcs) 二进制文件在 Windows 上运行 Mono 2.10:OK
- Linux 编译 (dmcs) 二进制文件在 Windows 上运行 MS.NET 4.0:OK
- Windows 编译 (dmcs) 二进制文件在带有 Mono 2.10 的 Windows 上运行:OK
- Windows 编译 (dmcs) 二进制文件在 Windows 上运行 MS.NET 4.0:OK
- Windows 编译 (csc.exe) 二进制文件在带有 Mono 2.10 的 Windows 上运行:OK
- Windows 编译 (csc.exe) 二进制文件在 Windows 上运行 MS.NET 4.0:OK
在 linux 上我只测试了 mono 本身编译的二进制文件,但我预计不会有任何问题。也许在 List<> 中存储动态有一些微妙的不同,我现在就测试一下
关于c# - ExpandoObject 中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7828962/