I am trying to serialise a struct that has a the following layout:
[StructLayout(LayoutKind.Explicit, Pack = 2)]
public readonly struct EntryInfo
{
[FieldOffset(0x00)]
[MarshalAs(UnmanagedType.U2, SizeConst = 2)]
public readonly ushort Type;
[FieldOffset(0x02), MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public readonly string Name;
}
Sadly, even though Pack=2
and MarshalAs(UnmanagedType.U2)
is specified, it still fails at runtime with:
System.TypeLoadException: Could not load type 'Structs.EntryInfo' from assembly 'MySharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 2 that is incorrectly aligned or overlapped by a non-object field.
The goal is keep using explicit field offsets (hence requiring Explicit layout type), yet still support packed/unaligned fields.
Setting the layout to Sequential
and keeping the Pack
option makes it work, but I have some structs where the data I know how to read starts at not at the beginning of the struct, hence I'd like to have to avoid having to use a sequential layout and creating padding fields.
If I set the layout to Sequential and keep the Pack
field, it all works, but I can't really see why the current code should not work, namely, I have specified the correct managed types, I did set the pack.
Based on Incorrectly-aligned/non-object field in struct I understand that the array should be DWORD aligned, but it does not explain why that is the case, or why that requirement goes away with Sequential mode.