Wednesday, 21 August 2013

F# attributes, typeof, and "This is not a constant expression"

F# attributes, typeof, and "This is not a constant expression"

Some .NET attributes require a parameter of type Type. How does one
declare these parameters in F#?
For example, in C# we can do this:
[XmlInclude(typeof(Car))]
[XmlInclude(typeof(Truck))]
But, in F# the following...
[<XmlInclude(typeof<Car>)>]
[<XmlInclude(typeof<Truck>)>]
...results in a compiler error: This is not a constant expression or valid
custom attribute value.

No comments:

Post a Comment