c# - 为什么编译器在仅传递字符串数组时由于非常量表达式而出错
问题描述
当我将字符串数组传递给这样的测试函数时:
[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 ) {
//...
}
解决方案
让我们将其简化为不涉及重载的东西,并删除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[]
它可以正常工作。因此,问题在于将该参数类型应用于参数。规范还说属性参数和参数“受与简单赋值相同的规则约束” - 但在这种情况下会很好。所以我在规范中看不到任何应该不允许这样做的东西。
推荐阅读
- node.js - firebase.database 不是函数节点 js
- ios - 使用泛型 Swift 的 Setter 和 Getter
- vbscript - 在 VBS 脚本中使用 SQL 命令
- r - 如何在r中对列表的值进行排名?
- node.js - 如何将 cookie 从 apollo-server 传递到 apollo-clenet
- migration - 如何在 Drupal 8 中设置基本 URL
- firebase - 具有后台任务的 Firebase 可调用函数
- azure - 测试迁移 Azure 迁移
- r - 在名称模式中使用迭代元素
- opencart2.x - Opencart facebook 扩展意外令牌