https://blog.csdn.net/u012846041/article/details/37518313

标准C或者C++中均提供关键字定义联合结构,C#中未提供类似的关键字,但仍然可以定义联合体结构,如下例:

[StructLayout(LayoutKind.Explicit)]

public struct PROCESSOR_INFO_UNION

{

[FieldOffset(0)]

internal uint dwOemid;

[FieldOffset(0)]

internal ushort wProcessorArchitecture;

[FieldOffset(2)]

internal ushort wReserved;

}

上例中:dwOemid是占用4个字节的整数;wProcessorArchitecture,wReserved分别是占用2个字节的短整数,wProcessorArchitecture在0字节处对齐;当使用dwOemid进行访问时,则直接使用四个字节,而访问wProcessorArchitecture和wReserved则分别使用两个字节

05-07 11:09