I'm trying to serialize an object in C#. I got the object size and saved it in a variable, size1 on line 207 in above screenshot. Size1 has a value of 160. Then I used size1 to allocate an array of bytes called buf in line 210. Buf comes out to be a 2 byte array! How can this be?!
The problem is here
byte[] buf = new byte[size1];
byte[] buf2 = new byte[16];
buf = b.ReadBytes(...); //<----
You are replacing buf
with the result of ReadBytes
. That throws away your original array and replaces it with the array that was returned from ReadBytes
(which in your case was a two byte array)
ReadBytes() returns a byte[]
. When you write
buf = b.ReadBytes(Marshal.SizeOf(firstRecord));
then buf
points at a completely different byte[]
that equals whatever b.ReadBytes()
returned.
It looks like you're trying to convert some object into a byte array, this answer may help. Convert any object to a byte[]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.