Using Marshal.SizeOf() method on a custom struct containing only value types

前端 未结 2 521
后悔当初
后悔当初 2020-12-07 04:05

I created a simple struct which consists of two value types.

public struct Identifier
{
    public Guid ID { get; set; }
    public Byte RequestType { get; s         


        
相关标签:
2条回答
  • 2020-12-07 04:31

    By default the CLR is allowed to rearrange (which for simple structs it never does) and pad structs as it pleases. This is typically to keep it aligned to word boundaries when in memory.

    If you don't like this behavior and want to change it, you can specify no packing as follows:

    [StructLayout(LayoutKind.Sequential,Pack=1)]
    
    0 讨论(0)
  • 2020-12-07 04:32

    Your Identifier struct is padded with 3 bytes when copied to unmanaged memory for alignment reasons.

    0 讨论(0)
提交回复
热议问题