1

我正在尝试完成这项工作,但我收到此错误..

因为它包含偏移量 0 处的对象字段,该对象字段未正确对齐或被非对象字段重叠。

[StructLayout(LayoutKind.Explicit)]
    public struct ListEntry {
        [System.Runtime.InteropServices.FieldOffset(0)]
        [MarshalAs(UnmanagedType.ByValArray, SizeConst=17)]
        public byte[] raw;
        [System.Runtime.InteropServices.FieldOffset(0)]
        public byte version;
        [System.Runtime.InteropServices.FieldOffset(1)]
        public UInt16 magic;
        [System.Runtime.InteropServices.FieldOffset(3)]
        public UInt32 start_time;
        [System.Runtime.InteropServices.FieldOffset(7)]
        public UInt16 run_id;
        [System.Runtime.InteropServices.FieldOffset(9)]
        public UInt16 channels;
        [System.Runtime.InteropServices.FieldOffset(11)]
        public UInt16 sampling_rate;
        [System.Runtime.InteropServices.FieldOffset(13)]
        public UInt32 start_sector;
    }
4

1 回答 1

2

也许作为一个固定大小的缓冲区?

[System.Runtime.InteropServices.FieldOffset(0)]
public fixed byte raw[17];

请注意,您需要将其视为byte*代码,例如:

byte* ptr = x.raw;
// now copy / inspect / whatever from ptr
于 2012-10-22T10:50:49.117 回答