Question

In C# I can do this:

[InlineData(new [] { "foo" })]

When I try to do this in F#:

[<InlineData([| "foo" |])>]

It gives me a compile error:

This is not a valid constant expression or custom attribute value

Am I doing something wrong or does F# not support arrays as attribute parameters?

UPDATE

The issue seems related to the signature of the attribute:

InlineDataAttribute(params dataValues: obj[]) : unit

The goal is to pass the string array as a single element in params array.

Was it helpful?

Solution

I think you might have already figured that part out, but if you do [<InlineData([| "foo" :> obj |])>], the compiler will stop complaining and accept the array you're passing in as the params array. It's not what you ask for in the edit (an array as a single item in the params array), but at least you have a way of passing a params array to an attribute.

I'm not sure if it's fair to say F# is in the wrong here - having arrays as attribute arguments is only possible if you mark them as not CLS-compliant. Some details here.

What you could try is to pass your array as the params array to the attribute, and have your test function accept a params array (by annotating the argument with [<ParamArray>]).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top