【发布时间】:2023-04-10 20:06:01
【问题描述】:
我有结构:
type OneDevice = {
mutable id : System.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
mutable pwd : byte array
mutable offsetInModbus : System.UInt16
mutable reserv : System.UInt16
}
而且我需要复制一些使用它作为字节数组。在 C# 中我可以在这里声明字节数组的大小,但现在我不知道 pwd 的大小。
我正在尝试使用:
let memcp(device : OneDevice, bytes : byte array) =
Array.zeroCreate <| Marshal.SizeOf(typeof<OneDevice>)
|> fun (array : byte array) ->
GCHandle.Alloc(array, GCHandleType.Pinned) |> fun handle ->
Marshal.StructureToPtr(device, handle.AddrOfPinnedObject(), true)
handle.Free()
但收到错误消息:
错误无法将类型“Model + OneDevice”打包为非托管 结构体;无法计算出有意义的大小或偏移量。
我认为这是因为我不知道这里的密码大小。那么如何在 F# Structure 上使用它呢?或者我可以以某种方式声明静态大小的数组类型?
谢谢
【问题讨论】:
标签: .net f# bytearray structure typeof