编译器错误 C3270

“field”: FieldOffset 特性只能在 StructLayout(Explicit) 上下文中使用,在该情况下它是必需的

使用 FieldOffset 标记了一个字段,仅在 StructLayout(Explicit) 生效时允许使用该特性。

下面的示例生成 C3270:

// C3270_2.cpp
// compile with: /clr /c
using namespace System::Runtime::InteropServices;

[ StructLayout(LayoutKind::Sequential) ]
// try the following line instead
// [ StructLayout(LayoutKind::Explicit) ]
public value struct MYUNION
{
   [FieldOffset(0)] int a;   // C3270
   // ...
};