将数组作为属性值传递

Pass an array as attribute-value

我有一个测试方法如下:

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

    return actual.SequenceEqual(expected);
}

但是我得到一个编译器错误:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.

编译器可能无法区分以下构造函数:

TestCase(params object[] args, Named Parameters);

TestCase(object ob1, Named Paramaters);

因为 new string[] { "1", "2", "5" } 可以解析为 params object[]object

来自 this post 我知道字符串数组应该可以作为编译常量传递。

如何向 TestCase 提供字符串数组?

我找到了一个使用参数方法的解决方案:

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

    return actual.SequenceEqual(expected);
}