title: Marshal Union(C ) with a Struct which contains an Array in C#
body: <p>I try to Marshal a unmanaged c++ dll in c#, but the marshaller fails when creating my union.</p>
  
  <p>Why does this code fail? </p>
  
  <pre><code>    [StructLayout(LayoutKind.Sequential)]
      public struct StructWithArray
      {
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
          public int[] MySimpleArray;
          //More Stuff
      }
  
      [StructLayout(LayoutKind.Explicit)]
      public struct Union
      {
          [FieldOffset(0)]
          public int Int; //Or anything else
          [FieldOffset(0), MarshalAs(UnmanagedType.Struct)]
          public StructWithArray MyStructWithArray;
          //More Structs
      }
  </code></pre>
  
  <p>And then constructing the Union:</p>
  
  <pre><code>Union MyUnion = new Union();
  </code></pre>
  
  <p>It fails if I run the code with the following Message: (Translated)</p>
  
  <blockquote>
    <p>{"The Type "Union" of the Assembly [...] could not be loaded because it contained an Objectfield at Offset 0, which is not aligned correctly or got overlapped by a field which isnt a ObjectField":"Union"}</p>
  </blockquote>
  
  <p>Any Suggestions?</p>
  
  <p>Ps: The original code is heavily simplified to show only the Problem. There are much more Structs, and the Union is also contained by another Struct.</p>