I have a class that I need to convert to a byte array for a Windows Mobile device. The byte array is sent using ssl to a server, which needs to rebuild the class. I have used BinaryFormatter on a PC client that worked great. However I can't use that with the compact framework. This is the code I am currently trying to use but its giving me the error message "Type 'PackageTrans' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.".
public byte[] ClassToBytearray(object obj)
{
int Length = Marshal.SizeOf(obj); <----------This is where I get the error.
byte[] bytearray = new byte[Length];
IntPtr ptr = Marshal.AllocHGlobal(Length);
Marshal.StructureToPtr(obj, ptr, false);
Marshal.Copy(ptr, bytearray, 0, Length);
Marshal.FreeHGlobal(ptr);
return bytearray;
}
This is my first time using Marshal. Why wouldn't a meaningful size or offset be computed? What is it that I am missing? Is there a different way I should be doing this? Any and all help would be greatly appreciated. Thanks -Kio
modified on Friday, June 11, 2010 1:25 AM