I'm not sure how this piece of code works.
[Serializable]
class Blah
{
public Blah(int value)
{
this.value = value;
}
public int value;
}
BinaryFormatter b = new BinaryFormatter();
Blah blah = new Blah(4);
MemoryStream s = new MemoryStream();
b.Serialize(s, blah);
s.Seek(0, SeekOrigin.Begin);
blah = null;
blah = (Blah)b.Deserialize(s);
As I don't have a parameterless constructor, it seems strange that the deserializer can create a new instance of Blah.
The deserialization process uses FormatterServices.GetUninitializedObject
which gets an object without calling any constructor.
The serializer doesn't call a constructor when it deserializes an object. The values of the fields are set directly. It doesn't need to create the object (via new
) it just creates storage, fills it, and casts it as a Blah
type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With