Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing a class containing a List<T>: Why is List initially filled with Nulls?

I have a class Bar which contains a List<Foo>, with both Foo and Bar implementing ISerializable.

When deserializing a Bar, the List<Foo> is initially filled with (the correct number of) nulls; then on exiting the Bar deserialization ctor, each Foo's deserialization ctor is called, filling the List<Foo> with the (correctly deserialized) Foos.

Why is this happening? I can't replicate it in a test project: whatever I have tried has resulted in the Foo deserialization ctors being called before the Bar ctor. This is actually the behaviour I would like, as I need the list to be filled in order to do some initialization for the deserialized Bar!

Anyone have an idea as to what could be causing the Foos to be deserialized so late? Thanks!

like image 958
Joel in Gö Avatar asked Mar 08 '10 11:03

Joel in Gö


1 Answers

It is logic. The deserializer deserialized it object by object, then following references. So, first it sets up the List with X spaces... which actually all are NULL.

Then it goes in and deserializes object by object, putting them into the proper references.

All check etc. logic from you should ONLY run AFTER deserialization has completed - per definition you have to always have partial / invalid states while the deserializer runs.

The issue why things are done that late is proabably that your test scenario is a lot easier than the real data, so something makes the serializer "turn the order" on the production side.

like image 150
TomTom Avatar answered Oct 17 '22 08:10

TomTom