Enums f#强制转换枚举元素类型

Enums f#强制转换枚举元素类型,enums,f#,Enums,F#,我有一个很简单的问题,我想,但我找不到任何关于它的信息。 例如,在c#中,我可以使用继承定义值枚举类型,如: public enum RequestedService : ushort { [Description("Unknown")] Unknown = 0, [Description("Status")] SERVICE1 = 0x0001 } 但我该如何用F#来表达呢?到目前为止,我把它翻译成: type RequestedService =

我有一个很简单的问题,我想,但我找不到任何关于它的信息。 例如,在c#中,我可以使用继承定义值枚举类型,如:

public enum RequestedService : ushort
{
    [Description("Unknown")]
    Unknown = 0,

    [Description("Status")]
    SERVICE1 = 0x0001
}
但我该如何用F#来表达呢?到目前为止,我把它翻译成:

type RequestedService =
    | [<Description("Unknown")>] Unknown = 0u
    | [<Description("Status")>] SERVICE1 = 0x0001u

但是我得到了编译错误,所以有可能吗?

将非
int32
值强制转换为
枚举
的过程记录在中:

默认函数适用于类型
int32
。因此,它不能与具有其他基础类型的枚举类型一起使用。相反,请使用以下命令

type uColor =
   | Red = 0u
   | Green = 1u
   | Blue = 2u
let col3 = Microsoft.FSharp.Core.LanguagePrimitives.EnumOfValue<uint32, uColor>(2u)
有关详细信息,请参阅:

type uColor =
   | Red = 0u
   | Green = 1u
   | Blue = 2u
let col3 = Microsoft.FSharp.Core.LanguagePrimitives.EnumOfValue<uint32, uColor>(2u)
open Microsoft.FSharp.Core.LanguagePrimitives

type RequestedService =
    | [<Description("Unknown")>] Unknown = 0u
    | [<Description("Status")>] SERVICE1 = 0x0001u

let service = EnumOfValue<uint32, RequestedService>(0u) // Or 1u or whatever
let service : RequestedService = EnumOfValue 0u // Or 1u or whatever