是否有更简单的方法来使用F#可变结构

cnd*_*cnd 0 f# structure

我现在怎么做真的很奇怪.可悲的是我有很多结构并经常使用它们.

以下是我的表现:

[<type:StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)>]
type OneDevice = {
        mutable id              : UInt16
        mutable typeDev         : byte
        mutable portNum         : byte
        mutable Parity          : byte
        mutable StopBits        : byte
        mutable BaudRate        : byte
        mutable addr1           : byte 
        mutable addr2           : byte 
        mutable useCanal        : byte
        mutable idGroup1        : byte
        mutable idGroup2        : byte 
        mutable idGroup3        : byte
        mutable idGroup4        : byte
        mutable idGroupSos1     : byte 
        mutable idGroupSos2     : byte 
        mutable idGroupSos3     : byte 
        mutable idGroupSos4     : byte 
        mutable idSosReserv     : byte 
        mutable addrModbus      : byte 
        mutable offsetModbus    : System.UInt16
        [<field: MarshalAs(UnmanagedType.ByValArray, SizeConst = 17)>]
        mutable pwd             : byte array
        mutable offsetInModbus  : UInt16
        mutable reserv          : UInt16 }
Run Code Online (Sandbox Code Playgroud)

这是我如何定义结构."type str = struct ..."的作品真的与众不同!我完全需要我的结构变体.但写这个只是我的一半麻烦,下半部分是我如何创建这个结构的新元素.让mutable dev = new OneDevice()不起作用!所以我需要这样做:

let mutable dev = {  
    id              = 0us 
    typeDev         = 0x00uy
    portNum         = 0x00uy
    Parity          = 0x00uy
    StopBits        = 0x00uy
    BaudRate        = 0x00uy
    addr1           = 0x00uy
    addr2           = 0x00uy
    useCanal        = 0x00uy
    idGroup1        = 0x00uy
    idGroup2        = 0x00uy
    idGroup3        = 0x00uy
    idGroup4        = 0x00uy
    idGroupSos1     = 0x00uy
    idGroupSos2     = 0x00uy
    idGroupSos3     = 0x00uy
    idGroupSos4     = 0x00uy
    idSosReserv     = 0x00uy
    addrModbus      = 0x00uy
    offsetModbus    = 0us
    pwd             = Array.zeroCreate 17
    offsetInModbus  = 0us
    reserv          = 0us }
Run Code Online (Sandbox Code Playgroud)

这是最奇怪的部分.我可以让它变得更容易吗?

谢谢 !

Dan*_*iel 5

问题是这些不是结构.他们是记录类型.使用type [<Struct>] MyStruct = ....

请参阅http://msdn.microsoft.com/en-us/library/dd233233.aspxhttp://msdn.microsoft.com/en-us/library/ee340416.aspx

记录类型:http://msdn.microsoft.com/en-us/library/dd233184.aspx

结构是值类型.记录是引用类型,如类.