2

我正在尝试像常规列表一样初始化不可变列表,但它告诉我它不需要 0 个参数。如果我传递 1 个参数、2 个参数等,它会引发相同的错误。

public static readonly ImmutableList<object[]> AddressTestCases = new ImmutableList<object[]>
{
    new object[]{ "", false },
    new object[]{ "testestest", true },
};

我在这里做错了什么?有没有办法在不使用 .Add 的情况下做到这一点?

4

3 回答 3

5

Ok ImmutableList有一个你应该使用的 create 方法

public ImmutableList<int> ImmutableListCreate()
{
    return ImmutableList.Create<int>(1, 2, 3, 4, 5);
}
于 2018-06-12T15:15:59.170 回答
0

To create an ImmutableList you have to use the static factory method Create() defined on the ImmutableList static class.

This means you will need

public static readonly ImmutableList<object[]> AddressTestCases = 
    ImmutableList.Create(new object[] { "", false }, new object[] { "testtest", true });
于 2018-06-12T15:20:05.243 回答
0

您使用的语法没有调用您认为的构造函数。它正在调用空构造函数,然后在后台调用.Add您提供的对象数组。

您将需要使用其中一种构建器方法:

public static readonly ImmutableList<object[]> AddressTestCases =
                          new[] {
                                   new object[]{ "", false }, 
                                   new object[]{ "testestest", true }
                                }).ToImmutableList();
于 2018-06-12T15:15:42.710 回答