[StructLayout(LayoutKind.Sequential, Pack = 1)] public struct RECT { public UInt32 left; public UInt32 top; public UInt32 right; public UInt32 bottom; } 程序里面有如上的一段代码,请问: [StructLayout(LayoutKind.Sequential, Pack = 1)] 这条语句是什么意思?
在使用Explicit排列方式时需要同时使用FieldOffset 来指明变量在内存中的偏移量 [StructLayout(LayoutKind.Explicit)] public class SYSTEM_INFO { [FieldOffset(0)] public ulong OemId; [FieldOffset(4)] public ulong PageSize; [FieldOffset(16)] public ulong ActiveProcessorMask; [FieldOffset(20)] public ulong NumberOfProcessors; [FieldOffset(24)] public ulong ProcessorType; }如果两个变量使用了同一个内存空间也就创建了在 C/C++ 中称为联合的布局
其中第一个参数表示类或结构的排列方式
包括Auto,Explicit,Sequential
Sequential 表示在内存中排列是顺序的
Pack = 1 是指的内存对齐方式,为一个字节对齐
在使用Explicit排列方式时需要同时使用FieldOffset 来指明变量在内存中的偏移量
[StructLayout(LayoutKind.Explicit)]
public class SYSTEM_INFO
{
[FieldOffset(0)] public ulong OemId;
[FieldOffset(4)] public ulong PageSize;
[FieldOffset(16)] public ulong ActiveProcessorMask;
[FieldOffset(20)] public ulong NumberOfProcessors;
[FieldOffset(24)] public ulong ProcessorType;
}如果两个变量使用了同一个内存空间也就创建了在 C/C++ 中称为联合的布局
Pack没接触过……