C# Deserialization and constructor initialization

This is an example how to recreate non serialized member of the class on deserialization.

class TestSerialization
{
  [Test]
  public void Test()
  {
    var parent = new Parent(2,3);
 
    // serialize
    byte[] data = SerializationHelper.Serialize(parent);
    // deserialize
    var parentCopy = SerializationHelper.Deserialize(data) as Parent;
 
    Assert.AreEqual(2, parentCopy.Value1);
    //confirm that constructor is not called on deserialization
    Assert.AreEqual(6, parentCopy.Value2);
  }
 
  [Serializable]
  class Parent
  {
    public Parent(int value1, int value2)
    {
      _value1 = value1;
      _value2 = value2 * 2;
     //Child object is not serialized, so needs to be recreated on deserialization (see below)
      _child = new Child() { Value1 = _value1, Value2 = _value2 };
    }
 
    private int _value1;
    private int _value2;
 
    [NonSerialized]
    private Child _child;
 
    public int Value1 { get { return _child.Value1; } }
    public int Value2 { get { return _child.Value2; } }
 
    [OnDeserialized]
    private void Init(StreamingContext context)
    {
      //this is the place where we recreate child object
      _child = new Child() { Value1 = _value1, Value2 = _value2 };
    }
  }
 
  //this object is not Serializable
  class Child
  {
    public int Value1 { get; set; }
    public int Value2 { get; set; }
  }
 
  public class SerializationHelper
  {
    private static readonly BinaryFormatter Fmt = new BinaryFormatter();
 
    public static byte[] Serialize(object obj)
    {
      if (obj == null)
        throw new ArgumentNullException("obj");
      // serialize to memory
      var s = new MemoryStream();
      Fmt.Serialize(s, obj);
      s.Flush();
      return s.ToArray();
    }
 
    public static object Deserialize(byte[] data)
    {
      var os = new MemoryStream(data);
      return Fmt.Deserialize(os);
    }
  }
}

Comments

  1. Your posts always helps me out to know everything on C#, on every post of yours I gets a opportunity to learn something new from it.

    ReplyDelete
  2. Thanks. the posts are the solutions to various problems I come across and these is where the diversity is coming from. I'm glad you find them useful. All the best.

    ReplyDelete
  3. Wow such a brilliant idea of the language. Thanks
    Ecommerce Website Development

    ReplyDelete
  4. Thanks for the great article here. I was searching for something like that for quite a long time and at last I have found it here. I hope to see more such nice articles in the nearest future too…

    ReplyDelete
  5. Thanks for sharing this amazing post. I am a Washington dc property appraiser at Aipraiser that is a professional real estate appraisal online portal that provides nationwide property and appraisal services.

    ReplyDelete

Post a Comment

Popular posts from this blog

Parse XML to dynamic object in C#

Tibco Rendezvous (tibrv) C# .Net example