title: Copying c++ struct in c#
body: <p>i'm working with C# and visual studio 2012.</p>
  
  <p>I have a program that recieves a c++ struct with data from tcp socket. In c++ program this struct is serialized, packed and sended in a byte array. </p>
  
  <p>The c++ struct has inheritance, so in first step i thought that using c# classes should be better, reading data with <code>BitConverter.ToSomething</code> but the problem comes with c++ padding, i know that visual c++ alignment is 8-byte, so knowing the type and the order of data i could arbitrary jump padding, but i'm trying to do this in a more automatically mode (easier user maintenance and not calculate again if the struct is changed).</p>
  
  <p>Then, i suppose the correct way is change c# classes to structs and duplicate data fields, so the question is: is there anyway to copy []byte to struct regardless of the padding? i saw different examples about how to copy to struct, but in all of them field offset and field size are arbitrary defined with perfect alignment.</p>
  
  <p>i.e:</p>
  
  <p>i'm recieving 80 bytes, 76 is my real struct size, so 4 bytes are padding to memory alignment, but i want the compiler to populate the c# struct knowing that if i recieve an integer and after that a double (i know the order and size of the fields), then there is where 4 extra bytes are put and they have not to be read. Like when you do the same in c++ using pointer to data.</p>