<mp********@gmail.com> wrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...
I want to save the raw data of a class (without the .NET object
overhead) to a binary file. For example:
ref class Test
{
public:
String^ name;
Int32 number;
....
}
I want to save the raw characters of the string and the raw integer
without the additional .NET Object data. I am trying to create an
array of Bytes and cast that array as my class, but the casting fails.
I was then going to write the Array out using a BinaryFormater object,
but I first need to cast the array as my class so I can easily stuff
the array. Can anyone help by providing an example of casting an array
of Bytes to a managed class, or of writing the actual raw data of a
class to a file?
That's not going to happen. However you can treat your object as an array
of bytes. But you must allocate the buffer with the class type. You would
also get further with a value class than a ref class.
This is by nature not verifiably type-safe. Count on using unsafe code
(pointers). Furthermore, the string is not contained inside your object,
only a gc pointer (reference), which can be modified by the gc at any time,
unless you use pin_ptr.
However, something like this should get you the binary data for any field:
Test^ that = gcnew Test();
// fill in that
pin_ptr<Int32> pRaw = &that->number;
BYTE* pBytes = (BYTE*)pRaw;