Question Copying an object changes its reference


New member
Nov 5, 2012
Programming Experience
I'm using an "object factory/pool" to store sprites. I have it set up so that:

ASprite tmp = spritePool.GetObject();
tmp.state = 133;

Will make a new object for tmp, then set its state to 133. All the objects in the pool are stored in an array.

Now, I want to populate that pool by deserializing several XML files. I've tried a few different approaches to this. What I'm aiming for is something like:

ASprite tmp = spritePool.GetObject();
tmp = (ASprite)Map.Streamer.DeSerialize(typeof(ASprite), file);

I've tried approaching this through the above code, but I didn't expect it to work. Whereas in the first snippet, spritePool.Data[0].state would be 133, the same data in the second code would be 0 (the default) instead of whatever the XML file specifies..

I also attempted this through reflection, and the MemberwiseClone method. Both had the same results.

How would I go about fetching objects from this pool and then changing them according to the serialization?

Top Bottom