Having a problem while deserializing a number of objects stored as BLOBs in a MySQL database.
Each object class has its own deserialize function.
Class1 c1Holder = (Class1)binFormatter.Deserialize(memStream);
Works fine, but then …
Class2 c2Holder = (Class2)binFormatter.Deserialize(memStream);
…calls the Class1 deserialize function. When I step through it using the VS 2010 debugger it will call the correct function on the 2nd attempt.
Also…
Class3 c3Holder = (Class3)binFormatter.Deserialize(memStream);
…calls the Class1 deserialize function, then the Class2 function and finally the correct function.
Any ideas?
From the comments:
This suggests that you are simply expecting the data to be a different type than it actually is. The point of BinaryFormatter is that if you serialize a Class7, then it will deserialize as a class7. With other serializers, you often need to tell it what to deserialize it as, allowing different types to be interchangeable as long as they look similar.
So with BinaryFormatter: if you want to get a Class2, serialize a Class2.
To be clear, the following:
does not say "deserialize this as a Class2" – it is more like:
i.e. "serialize it into whatever it is, then cast that as a Class2". If it isnt a Class2 it will fail. This isn’t a conversion etc – is is a type-check only.
If you want advice on serialization that works with similar-but-different typed, let me know.