title: Error on StuctureToPtr
body: <p>I have a binary files that were made with <code>structs</code>. I'm getting this error when trying to write a <code>struct</code> back to the binary file: <code>Type could not be marshaled because the length of an embedded array instance does not match the declared length in the layout.</code></p>
  
  <p>Here's the <code>struct</code>, which should be <code>2368 bytes</code>. Confirmed this by doing <code>Marshal.SizeOf(typeof(DATA))</code> and basic math.</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
  public struct DATA
  {
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
      public char[] GD;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
      public char[] Label; 
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
      public char[] Feat;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
      public char[] Wor;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
      public char[] Source; 
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
      public char[] Anam;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
      public char[] Crete;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
      public char[] Miss;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
      public char[] Best;         
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
      public char[] Xware;    
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
      public char[] XVersion;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
      public char[] UncertX;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
      public char[] UncertY;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
      public char[] Descrip;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
      public char[] Discrim; 
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]
      public char[] Reserved;
  }
  </code></pre>
  
  <p>I made a simple <code>char[]</code> to test:</p>
  
  <pre><code>char[] testArray = new char[] { 'a', 'b', 'c', 'd' };
  DATA testStruct = new DATA(); //declare the struct
  testStruct.Miss = testArray; //set one of the fields to the char[]
  </code></pre>
  
  <p>Then I write to the binary file:</p>
  
  <pre><code>using (var fStream = new FileStream(filePath, FileMode.Append))
  {
      WriteRampToFile(gisDA, fStream );
  }
  
  /// &lt;summary&gt;
  /// Writes byte array containing a given struct to a file
  /// &lt;/summary&gt;
  public void WriteRampToFile&lt;T&gt;(T str, FileStream fStream )
  {
      byte[] buffer = GetBytes(str); //puts struct into byte array
      fStream .Write(buffer, 0, Marshal.SizeOf(typeof(T))); //write byte     array to file
  }
  
  /// &lt;summary&gt;
  /// Converts the given struct and returns it as a byte array
  /// &lt;/summary&gt;
  /// &lt;param name="str"&gt;A structure defined at the top of this file&lt;/param&gt;
  public byte[] GetBytes&lt;T&gt;(T str)
  {
      int size = Marshal.SizeOf(str);
      byte[] arr = new byte[size];
      GCHandle h = default(GCHandle);
  
      try
      {
          h = GCHandle.Alloc(arr, GCHandleType.Pinned);
  
          Marshal.StructureToPtr(str, h.AddrOfPinnedObject(), false); //Error     here
      }
      finally
      {
          if (h.IsAllocated)
          {
              h.Free();
          }
      }
      return arr;
  }
  </code></pre>