title: How to marshal a struct into a UInt16 Array
body: <p>I know that you can use code like this to marshal a structure into a byte array:</p>
  
  <pre><code>public static byte[] StructureToByteArray(object obj)
  {
      int len = Marshal.SizeOf(obj);
      byte[] arr = new byte[len];
      IntPtr ptr = Marshal.AllocHGlobal(len);
      Marshal.StructureToPtr(obj, ptr, true);
      Marshal.Copy(ptr, arr, 0, len);
      Marshal.FreeHGlobal(ptr);
      return arr;
  }
  </code></pre>
  
  <p>But how do you marshal a structure into an array containing 16 bit words instead of bytes?</p>
  
  <pre><code>public static UInt16[] StructureToUInt16Array(object obj)
  {
      // What to do?
  }
  </code></pre>