我正在尝试在这篇msdn文章中创建以下结构。我正在尝试学习整个 FieldOffset 但不知道从哪里开始。
我基本上做了这样的事情。
[StructLayout(LayoutKind.Explicit, Size=12)]
public struct DHCP_OPTION_DATA_ELEMENT {
[FieldOffset(0)]
public DHCP_OPTION_DATA_TYPE OptionType;
[FieldOffset(4)]
public byte ByteOption;
[FieldOffset(4)]
public uint WordOption;
[FieldOffset(4)]
public UInt32 DWordOption;
[FieldOffset(4)]
public UInt32 DWordDWordOption;
[FieldOffset(4)]
public uint IpAddressOption;
[FieldOffset(4)]
public IntPtr StringDataOption;
[FieldOffset(4)]
public DHCP_BINARY_DATA BinaryDataOption;
[FieldOffset(4)]
public DHCP_BINARY_DATA EncapsulatedDataOption;
[FieldOffset(4)]
public string Ipv6AddressDataOption;
}
但是,它向我咆哮,说明了以下例外。
it contains an object field at offset 4 that is incorrectly aligned or
overlapped by a non-object field.