0

Using BinaryFormatter, how could I override the deserialization method of a class so that it would return an existing instance of this class instead of create a new one?

tbkn23
  • 5,205
  • 8
  • 26
  • 46
  • I don't think that it is possible because deserialization process always creates new object from data. You can try with Custom Serialization for your need [link](http://msdn.microsoft.com/en-us/library/ty01x675%28v=vs.110%29.aspx) – PratikP24 Dec 12 '13 at 06:20
  • Custom serialization is what I'm trying to do, however since the deserializer is a constructor, I don't know how I can return an existing instance instead of creating a new one... – tbkn23 Dec 12 '13 at 06:21
  • not sure why you want to do this but you can return an object reference insteading of performaing deserialization – Vinay Pandey Dec 12 '13 at 08:10
  • I'm using the default serialization, and at some point some classes contain reference to class A. Now when I deserialize class A, I want to check if this specific instance has already been deserialized, and if it has, I want to return that instance instead of creating a new one. – tbkn23 Dec 12 '13 at 09:34
  • It seems like you're confusing the terms "class" (an object-type definition) and "object" (a realized class instance, with data, in memory). That said, it looks like you're looking for a caching solution, not a (de)serialization solution. – Marc L. May 27 '14 at 16:31
  • I'm not looking for a cache (although a deserialization cache might be a solution). Say I have one child object in memory, with two parent objects (father & mother) referencing it. I serialize both parents. I then deserialize them. Now instead of having one child object referenced by both parents, I have two child objects, and each parent has a different child. I want them both to reference the same child after deserializing, just like before. – tbkn23 Jun 13 '14 at 05:02

0 Answers0