Advertisement Jump to content


This topic is now archived and is closed to further replies.

Roof Top Pew Wee

Re-establishing references after deserialization

This topic is 5364 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I have the following in C#:

// class A, B, and C already defined, A and B having one C member

A myA = new A();
B myB = new B();
C myC = new C();

myA.c = myC;
myB.c = myC;

Here obviously, myC, myA.c, and myB.c all point to the same memory, and changing one will result in changes in all. But let's say that that all objects are Serializable() and I save an object containing one A and one B. If the code is ran from above before saving, my understanding is that in the file, myA and myB will have their own copy of .c, and upon deserialization, myA.c will no longer equal myB.c, as they are saved "by value". This is at least least my understanding of the situation, so I would be glad to be corrected if I am wrong. However, if I am right, the problem then arises that the .c variable which should be always the same for both myA and myB now exists in memory twice. And changes made in myA.c will no longer show up in myB.c, after deserialization, of course. So my question is first, am I right in what I have stated above, and if so, is there any "built in" way to preserve these references, or will I have to do it manually with some sort of unique ID method and a reestablishment of references after loading? I hope I've been clear enough. Thanks in advance, --Vic-- [edited by - Roof Top Pew Wee on May 9, 2004 6:15:08 PM]

Share this post

Link to post
Share on other sites
The serialization facilities'' interface in .NET work via serializing roots of graphs of objects. If A and B are serialized as part of the same graph the shared reference should be stored. If A and B are serialized separately then separate copies should be stored. i.e. if you find yourself doing something like:

formatter.Serialize(stream, myA);
formatter.Serialize(stream, myB);

then the C objects will be deserialized as separate objects, but if you do something like:

formatter.Serialize(stream, SomeOtherObject);

where SomeOtherObject has references to both myA and myB, then when deserialized myA.c and myB.c should refer to the same object.

(This is assuming the default serialization routines are used, etc.)

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!