MemoryStream deserialized

M

M D

Please tell me why this:

static void Main(string[] args) {
Bid bid1 = new Bid(23,10.0F,3,0,DateTime.Now);
BinaryFormatter formatter = new BinaryFormatter();
Stream depot = new MemoryStream();
formatter.Serialize(depot, bid1);
Console.WriteLine("size of a bid- {0}", depot.Length);
Console.WriteLine("bid1 {0}", bid1);
Console.WriteLine(depot.CanRead);
Bid bid2 = (Bid)formatter.Deserialize(depot);
Console.WriteLine("bid2 {0}", bid2);
depot.Close();
}

results in this:

...\bin\Debug>consoleapplication1
size of a bid- 226
bid1 23:10:3:0:052405160487
True

Unhandled Exception:
System.Runtime.Serialization.SerializationException: End of
Stream encountered before parsing was completed.

Bid is:
struct Bid {
int p;
float o;
int b;
short w;
string tS;

which, in a constructor, converts the DateTime to a formatted string.

I've tried commenting out the depot.Close and setting the initial size
to 226 with identical results.


thx
md
 
M

Mickey Williams

You must rewind the depot stream back to the beginning after serializing
bid1 -- otherwise, you're at the end of the stream. Try something like this
before you call Deserialize:

depot.Seek(0, SeekOrigin.Begin);
 
M

M D

Yes, thank you. I surmised as much and tried depot.Position = 0 before
the post even showed up.

thx
md
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top