struct serializing into byte[]

Mister E

Centurion
Joined
Jul 23, 2004
Messages
136
Lets say I have a serialized struct called MyStruct, to write it out in binary form I know that I can do this:

Code:
FileStream fileStream = new FileStream(@"C:\wow.dat",FileMode.Create);
BinaryFormatter binaryFormatter = new BinaryFormatter(  );
binaryFormatter.Serialize(fileStream, MyStruct);

fileStream.Close();
But lets say, instead of sending it to a file, I wanted to just serialize it directly into a byte[]. How could I do that?

Thanks...
 
Easiest way is to use a MemoryStream
C#:
BinaryFormatter binaryFormatter = new BinaryFormatter(  );
byte[] b = new byte[100];  //make correct size
MemoryStream ms = new MemoryStream(b);
binaryFormatter.Serialize(ms, MyStruct);
 
Back
Top