.NET Binary Serialize object with references to other objects . . . what happens? -
if have object instance references other objects (for example instances b , c), , binary serialize a file, happens? have serialized data includes a, b , c?
how work exactly? if deserialize data? a, b, , c??
(feel free include internal workings explanations well).
all of references other objects serialized well. if deserialize data, end complete, working set of data, including objects a, b, , c. that's primary benefit of binary serialization, opposed xml serialization.
if of other classes object holds reference not marked [serializable]
attribute, you'll serializationexception
@ run-time (the image of shamelessly stolen web; run-time errors don't anymore in current versions of vs):
further that, i'm not sure "internal things" hoping understand. serialization uses reflection walk through public , private fields of objects, converting them stream of bytes, written out data stream. during deserialization, inverse happens: stream of bytes read in data stream, used synthesize exact replicate of object, along type information. of fields in object have same values held before; constructor not called when object deserialized. easiest way think you're taking snapshot-in-place of object, can restore original state @ will.
the class responsible actual serialization , deserialization called formatter (it inherits iformatter
interface). it's job generate "object graph", generalized tree containing object being serialized/deserialized root. mentioned above, formatter uses reflection walk through object graph, serializing/deserializing object references contained object. formatter intelligent enough know not serialize object in graph more once. if 2 object references point same object, detected , object serialized once. , other logic prevents entering infinite loop.
of course, it's easy have general understanding of how process works. it's much harder write code implements yourself. fortunately, that's been done you. part of point of .net framework complicated serialization logic built in, leaving free worrying it. don't claim understand of myself, , don't need either take full advantage of functionality offers. years of writing code hand over. should rejoicing, rather worrying implementation details. :-)
Comments
Post a Comment