title: Convert Object To Byte Array without Serialization nor Padding
body: <p>It seems easy but I can't see it easy at all. My question is I have a struct and I need to convert it to byte stream without any additional bytes for types nor padding nor metadata. Assume I have a struct </p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential)]
  public struct MyStruct
  {
      public ushort a;                 
      public uint b;  
      public uint c;
      public ushort d;
  }
  </code></pre>
  
  <p><strong>Remark:</strong> I can't change the pack here to 1(Project constrains), so using the following solution <strong>won't work</strong> as there's a padding added</p>
  
  <pre><code>int size = Marshal.SizeOf(typeof(T));
  byte[] arr = new byte[size];
  IntPtr ptr = Marshal.AllocHGlobal(size);
  Marshal.StructureToPtr(str, ptr, true);
  Marshal.Copy(ptr, arr, 0, size);
  Marshal.FreeHGlobal(ptr);
  </code></pre>
  
  <p><strong>Also Remark:</strong> I can't use Binary format serialization as there's a metadata added.  All what I want simply if a=1 b=2 c=3 d=4 to get binary format in a <strong>GENERIC WAY</strong> as </p>
  
  <pre><code>    arr {byte[12]}  byte[]
      [0]  1  byte
      [1]  0  byte
      [2]  2  byte
      [3]  0  byte
      [4]  0  byte
      [5]  0  byte
      [6]  3  byte
      [7]  0  byte
      [8]  0  byte
      [9]  0  byte
      [10] 4  byte
      [11] 0  byte
  </code></pre>
  
  <p>Any help?</p>