Edit: Thanks to @Sweeper for pointing out that the particular issue encountered in my example below has already been addressed in Jon Skeet's answer to this SO question. This does not directly answer the question, but does provide the workarounds necessary due to what appears to be a compiler bug.
Original Question: I am having trouble reconciling some behavior I have observed with attribute argument types with what is stated in the official documentation. In Marc Gravell's answer to this SO question, he quotes from ECMA 334v4 where it says:
§24.1.3 Attribute parameter types
The types of positional and named parameters for an attribute class are limited to the attribute parameter types, which are:
- One of the following types:
bool
,byte
,char
,double
,float
,int
,long
,short
,string
.- The type
object
.- The type
System.Type
.- An enum type, provided it has public accessibility and the types in which it is nested (if any) also have public accessibility.
- Single-dimensional arrays of the above types.
This MSDN tutorial on attributes also states that
Parameters to an attribute constructor are limited to simple types/literals:
bool
,int
,double
,string
,Type
, enums, etc and arrays of those types.
(emphasis mine in both quotes)
However, when I attempt to declare an attribute parameter of type string[]
, I get this error:
An attribute argument array creation must be single-dimension array of 'object[]' type
// OK
[TestCase(new[] {1, 2, 3, 5, 8, 13})]
public void TestIntArrayArgs(int[] intArr)
{
// Run test . . .
}
// Compiler error:
// "An attribute argument array creation must be single-dimension array of 'object[]' type"
[TestCase(new[] {"A", "B"})]
public void TestStringArrayArgs(string[] strArr)
{
// Run test . . .
}
I should note that if I wanted only one array of strings, using a params[]
argument for the test method would suffice; however, I want two arrays.
Can anyone spot what I am missing?