我有:
type Package =
abstract member Date : int
abstract member Save : unit -> unit
[<type:StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)>]
type Instant(date : int, value : int) =
let mutable _date = date
let mutable _value = value
member X.Value : int = _value
interface Package with
member X.Date : int = _date
member X.Save() = ...
但是获取错误:只能给出" structlayout"属性
的结构和类别的结构和类所以我意识到这一定是一样的:
type Instant =
struct
val Date : byte array
...
但是,这样我就丢失了界面。例如,在C#中,对于此类类添加type:StructLayout
是可能的(我认为)。我必须如何重构代码以避免此错误?
正如错误消息所说,StructLayout
应与显式构造函数一起使用:
type Package =
abstract member Date : int
abstract member Save : unit -> unit
[<type:StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)>]
type Instant =
val mutable Date: int
val mutable Value: int
new(date, value) = {Date = date; Value = value}
interface Package with
member x.Date = x.Date
member x.Save() = x.Value |> ignore
如果您对FieldOffset
有进一步的问题,请在此处查看代码示例。