title: convert a class to byte array + C#
body: <p>How can I convert a Class to byte array in C#. This is a managed one so the following code is failing</p>
  
  <pre><code>int objsize = System.Runtime.InteropServices.Marshal.SizeOf(objTimeSeries3D);
  byte[] arr = new byte[objsize];
  IntPtr buff = System.Runtime.InteropServices.Marshal.AllocHGlobal(objsize);
  System.Runtime.InteropServices.Marshal.StructureToPtr(objTimeSeries3D, buff, true);
  System.Runtime.InteropServices.Marshal.Copy(buff, arr, 0, objsize);
  System.Runtime.InteropServices.Marshal.FreeHGlobal(buff);
  </code></pre>
  
  <p>Thanks</p>