C# 将数组作为属性值传递

C# 将数组作为属性值传递,c#,attributes,nunit,C#,Attributes,Nunit,我有一个测试方法如下: [TestCase(new string[] { "1", "2", "5" }, Result = true)] bool AllocateIDsTest1(IEnumerable<string> expected) { var target = ... var actual = target.AllocateIDs(expected); return actual.SequenceEqual(expected); } 及 因为新

我有一个测试方法如下:

[TestCase(new string[] { "1", "2", "5" }, Result = true)]
bool AllocateIDsTest1(IEnumerable<string> expected)
{
    var target = ...
    var actual = target.AllocateIDs(expected);

    return actual.SequenceEqual(expected);
}

因为
新字符串[]{“1”、“2”、“5”}
可以解析为
参数对象[]
对象

从中我知道字符串数组应该可以作为编译常量传递


如何向
TestCase
提供字符串数组

我使用params方法找到了一个解决方案:

[TestCase("1", "2", "5", Result = true)]
public bool AllocateIDsTest1(params string[] expected)
{
    var target = ...
    var actual = target.AllocateIDs(expected);

    return actual.SequenceEqual(expected);
}

很好的解决方法。另一个选项是使用TestCaseSource,请参阅文档。甚至不是解决方法。:-)这是TestCase应该如何使用的。编译器错误并不是因为您传入了一个数组——这在属性上是明确允许的。这是因为您要求属性构造函数使用new动态创建数组。C#不允许这样做。@Charlie那么,当你说“在属性上明确允许这样做”时,将数组分配给属性究竟应该如何工作呢?通过
静态
字段?
TestCase(object ob1, Named Paramaters);
[TestCase("1", "2", "5", Result = true)]
public bool AllocateIDsTest1(params string[] expected)
{
    var target = ...
    var actual = target.AllocateIDs(expected);

    return actual.SequenceEqual(expected);
}