Tolu
Tolu

Reputation: 1147

Odd attribute behavior

I have a curious situation with attributes. I'm working with xUnit's InlineData attribute which takes a params of objects (params object[]). If I feed it an array and a string, all seems well:

[InlineData(new string[] { "Yada", "Yada yada" }, "Yada yada yada")]

... but if I feed it just an array:

[InlineData(new string[] { "Yada", "Yada yada" })]

... then I get this error:

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

What could the issue be?

Upvotes: 1

Views: 111

Answers (1)

Jamiec
Jamiec

Reputation: 136174

I'll highlight the important bit of the error message

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

The parameter type is object[] so you can't pass it string[].

In the first example you're implicitly passing an array of objects, one being a string array the other being a string... in the first example you're effectively doing this:

[InlineData(new object[]{new string[] { "Yada", "Yada yada" }, "Yada yada yada"})]

In the second example, you could have done this

[InlineData(new object[] { "Yada", "Yada yada" })]

Upvotes: 2

Related Questions