F # passing array to attribute

In C #, I can do this:

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

When I try to do this in F #:

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

This gives me a compilation 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 problem is with the attribute signature:

InlineDataAttribute(params dataValues: obj[]) : unit

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

+4
source share
1 answer

I think you might already understand this part, but if you do [<InlineData([| "foo" :> obj |])>], the compiler will stop complaining and accept the array you pass as the params array. This is not what you are asking for in editing (an array as one element in a params array), but at least you have a way to pass an array of params to an attribute.

, , F # - , CLS-. .

, , - params , params ( [<ParamArray>]).

+1

Source: https://habr.com/ru/post/1537081/


All Articles