I have a class that has an Explicit StructLayout. In the constructor
below, I am copying data from a byte array onto this class so as to
initialize all the explicit attributes of the class. What I don't
like is the overhead of allocating memory for a temporary variable
(pData) when I want to marshal the data in the rawData array directly
into the current object. Would there be a way to get an IntPtr
directly to the current object?
public EntityState(byte[] rawData)
{
IntPtr pData = Marshal.AllocHGlobal(rawData.Length);
Marshal.Copy(rawData, 0, pData, 144);
Marshal.PtrToStructure(pData, this);
Marshal.FreeHGlobal(pData);
}