There is no documented way to discover a mock managed structure. The JIT compiler easily uses this; it will reorder the fields of the structure to get better packaging. Marshaling should always get a predictable layout, as indicated by the [StructLayout] . You have to jump through the Marshal.StructureToPtr() hoop. Regardless of whether you do it yourself or let the pinvoke Marxiller do it for you.
Marshal.SizeOf(Type) gives the size of the struct marshaling. More information on why it works this way is available in this answer .
Hans passant
source share