How to pass a Type as an attribute parameter using F# syntax?

血红的双手。 提交于 2019-12-11 00:54:59

问题


FsCheck allows a custom Arbitrary in its NUnit integration:

[<Property(Verbose = true, Arbitrary= [typeof<Test.Arithmetic.MyArb>])>]
static member  MultiplyIdentity (x: int64) = x * 1 = x

This syntax doesn't work. I feel a bit embarrassed to ask, but apparently I never needed this before: how do you specify the type in F# as an attribute parameter? Microsoft says nothing about it, nor does the Wikibooks project and I have some trouble googling this (the word type is omnipresent).

Note 1: the Arbitrary parameter is of type Type [].


回答1:


I think you're close, but [1;2;3] creates a list<int>, you want an array literal using [| 1;2;3 |]:

[<Property(Verbose = true, Arbitrary= [| typeof<Test.Arithmetic.MyArb> |])>]
static member  MultiplyIdentity (x: int64) = x * 1 = x


来源:https://stackoverflow.com/questions/40997380/how-to-pass-a-type-as-an-attribute-parameter-using-f-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!