Adding items to an existing list during deserialization

Aug 5, 2012 at 2:51 PM
Edited Aug 5, 2012 at 2:52 PM

I have custom child collections defined in my class that get instantiated when an object is instantiated. Is it possible during deserialization to tell json.net NOT to instantiate a new collection but to just add the deserialized collection items into the existing collection?  The code I'm talking about is similar to below, you can see Im passing a function through to the constructor of the collection which it uses to build and compile an expression, I dont really want to have to figure out how to get that creation logic happening in the serializer. Thanks!

 

public class ClassWithChildCollection: DocumentRootBase
{

    private CustomCollection<TestClass1> _childCollection;

    public CustomCollection<TestClass1> ChildCollection
    {
      get
      {
        return _childCollection;
      }
    }

    public ClassWithChildCollection()
    {
       _childCollection= new CustomCollection<TestClass1>(this, x => x.TestClass1Property);
    }

}
Aug 6, 2012 at 12:42 AM

Ok well I feel a bit silly for asking this now because it turns out it was a simple fix - just had to set ObjectCreationHandling on the serializer to Reuse/Auto. Argh!! Thanks.