[Mono-list] serialization

Lawrence Pit loz@cable.a2000.nl
Thu, 25 Apr 2002 02:21:13 +0300


> For objects with [Serializable], this should not be hard, as I am pretty
> sure that this is done using the public field names (we just need to
> write a test and find out if this hypothesis is correct).

private fields also end up in serialized objects.



using System;

[Serializable]
public class Foo
{
     public int fooooooooooooo = 6;
     private int baaaaaaaaaaaaar = 5;
}



using System.Xml.Serialization;
using System;
using System.Net;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public class FooTest
{
  public static void Main()
  {

     BinaryFormatter f = new BinaryFormatter();
     Stream stream = File.OpenWrite("footest.bin");
     f.Serialize(stream, new Foo());
     stream.Close();

     BinaryFormatter f2 = new BinaryFormatter();
     Stream stream2 = File.OpenRead("footest.bin");
     Foo c = (Foo) f2.Deserialize(stream2);
     Console.Write("GOT : " + c.ToString());
  }
}



Greets,
Lawrence