我已经定义了以下结构来模拟C++联合(最终将用于C++ Interop):
[StructLayout(LayoutKind.Sequential)]
internal struct STRUCT1
{
public Guid guid;
public String str1;
public String str2;
}
[StructLayout(LayoutKind.Sequential)]
internal struct STRUCT2
{
public Guid guid;
public String str1;
public String str2;
public Int32 i1;
}
[StructLayout(LayoutKind.Explicit)]
internal struct MASTER_STRUCT_UNION
{
[FieldOffset(0)]
public STRUCT1 Struct1;
[FieldOffset(0)]
public STRUCT2 Struct2;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MASTER_STRUCT
{
public MASTER_STRUCT_UNION Union;
}
Run Code Online (Sandbox Code Playgroud)
我编写了以下测试代码,它将值分配给Struct1.guid并测试相等性Struct2.guid:
class Class1
{
public static void Test()
{
MASTER_STRUCT ms = new MASTER_STRUCT();
bool match;
ms.Union.Struct1.guid = …Run Code Online (Sandbox Code Playgroud)