在MSDN中union的使用示例如下:
C中申明为:
union MYUNION2
{
    int i;
    char str[128];
};C#申明为:
[ StructLayout( LayoutKind.Explicit, Size=128 )]
public struct MyUnion2_1 
{  
   [ FieldOffset( 0 )]
   public int i;
}
[ StructLayout( LayoutKind.Sequential )]
public struct MyUnion2_2 
{  
   [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=128 )] 
   public String str;
}有没有办法把他写为一个struct.
[ StructLayout( LayoutKind.Explicit, Size=128 )]
public struct MyUnion 
{
[FieldOffset( 0 )]
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=128)]
public string str;
[FieldOffset( 0 )]
public int i;
}
这种写法是错误的.原因:在托管代码中,值类型和引用类型不允许重叠。