更新时间:2023-02-16 21:14:20
您正在运行到一个字节对齐的问题。在试图保持对字边界字段的访问速度,编译器填充您的字符串
3额外的字节。为了解决这个问题,可以使用 StructLayoutAttribute
。
You're running into a byte-alignment issue. In an attempt to keep fields on word boundaries for speed of access, the compiler is padding your string
with 3 extra bytes. To fix this, use the Pack
field of the StructLayoutAttribute
.
[StructLayout(LayoutKind.Sequential, Pack=1)] // notice the packing here
public struct SFHeader
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
public string FileName;
public int Offset;
public short Size;
public byte Flags;
public byte Source;
public long LastWriteTime;
}