首页 > 解决方案 > 为什么编译器在仅传递字符串数组时由于非常量表达式而出错

问题描述

当我将字符串数组传递给这样的测试函数时:

[TestCase( new string[] { "1", "2", "3" }, 1 )]
[TestCase( new string[] { "54", "508" }, 1 )]
[TestCase( new string[] { "768" }, 2 )]
public void someTest( string[] someStrings, int someNumber ) {
    //...
}

编译工作正常。

但是,如果我删除整数参数,如以下代码片段所示:

[TestCase( new string[] { "1", "2", "3" } )]
[TestCase( new string[] { "54", "508" } )]
[TestCase( new string[] { "768" } )]
public void someTest( string[] someStrings ) {
    //...
}

出现带有该消息的编译器错误An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

我基本上得到了错误的原因,数组不是常量类型。但是,如果有另一个参数传递给测试函数,为什么编译器会接受一个数组?如果我在 TestCase 中放置另一个数组,它甚至可以工作:

[TestCase( new string[] { "1", "2", "3" }, new int[] { 1, 2, 3 } )]
[TestCase( new string[] { "54", "508" }, new int[] { 54, 508 } )]
[TestCase( new string[] { "768" }, new int[] { 768 } )]
public void someTest( string[] someStrings, int[] someNumbers ) {
    //...
}

标签: c#visual-studio-2013nunit

解决方案


让我们将其简化为不涉及重载的东西,并删除params

using System;

[AttributeUsage(AttributeTargets.All)]
class FooAttribute : Attribute
{
    public FooAttribute(object[] args)
    {
    }
}

// Error
[Foo(new string[] { "a", "b" })]
class FooTest1 {}

// Error
[Foo(new[] { "a", "b" })]
class FooTest2 {}

// Error
[Foo(args: new string[] { "a", "b" })]
class FooTest3 {}

// Error
[Foo((object[]) new string[] { "a", "b" })]
class FooTest4 {}

// Works
[Foo(new object[] { "a", "b" })]
class FooTest5 {}

// Works
[Foo(new[] { (object) "a", "b" })]
class FooTest6 {}

基本上,编译器不愿意为属性string[]中的object[]参数提供 a,即使这通常没问题。

相信这是一个编译器错误,检查了规范 - 但我不想肯定地说。该表达式new string[] { "a", "b" }确实算作规范术语中的属性参数表达式- 如果您将参数类型更改为string[]它可以正常工作。因此,问题在于将该参数类型应用于参数。规范还说属性参数和参数“受与简单赋值相同的规则约束” - 但在这种情况下会很好。所以我在规范中看不到任何应该不允许这样做的东西。


推荐阅读