Jump to content
  • Advertisement
Sign in to follow this  
djz

[.net] C# - Instatiation of Non-Serialized objects during Serialization

This topic is 2989 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

[Serializable]
public class foo
{
   private int foo;
 [NonSerialized]  
   private byte []bar;

   public foo()
   {
      bar = new byte[128];  // we need to perform this on deserialization too!
   }
}

I have been trying to figure this one out, or at least figure out what google terms to use: basically, I have an object with a buffer that I currently serialize; but the buffer can get quite large, and there's no real reason to serialize it. But it needs to be reset to a new reference on serialization. Currently, if I deserialize my object, I get an exception later on, because bar[] has not been instantiated. I'm sure there must be a simple & elegant way to do this (rather than deserializing, and then looping through all deserialized objects with buffers and reseting them)

Share this post


Link to post
Share on other sites
Advertisement
You didn't mention which serializer you are using, if it is the Soap- or BinaryFormatter and not the XmlSerializer, the easiest way would be to mark a method with the OnDeserializedAttribute and initialize your buffer there:


[OnDeserialized]
internal void InitFields(StreamingContext context) {
bar = new byte[128];
}

Share this post


Link to post
Share on other sites
Quote:
Original post by itachi
You didn't mention which serializer you are using, if it is the Soap- or BinaryFormatter and not the XmlSerializer, the easiest way would be to mark a method with the OnDeserializedAttribute and initialize your buffer there:


[OnDeserialized]
internal void InitFields(StreamingContext context) {
bar = new byte[128];
}


I am using the BinaryFormatter; and this is eventually how I ended up doing it. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!