c# - ExpandoObject 中的列表

标签 c# expandoobject

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)

解决方案更新: enter image description here

因为我在本地窗口中看不到 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/

相关文章:

c# - MSTest 中的数据驱动测试 - TestContext.DataRow 的问题

c# - 在 FireFox 中,如何从 JavaScript 调用 C# dll?

c# - 在没有基础字典结构的 PowerShell 中使用 C# ExpandoObjects(动态)

c# - 从性能的角度来看,ExpandoObject 与 Dictionary?

c# - 跨 dll 边界访问匿名/动态类型的属性会产生 RuntimeBinderException

c# - 缩短 if 表达式

c# - 异步模块或处理程序已完成,而异步操作仍未决

c# - 如何比较两个数据表

c# - 类型转换动态对象并传递到 UnitOfWork 和存储库模式。抛出异常

c# - 在 C# 中从 JSON 对象转换为 expando 对象