Monday, August 26, 2002

Another Serialization Bug

My colleague Stojan reported and isolated this one. If you serialize an array of structures that implement ISerializable, they will not deserialize correctly.

Sample code:

[Serializable]
public struct TestStruct : ISerializable {

    public bool Ok;

    public TestStruct (bool ok) {
        Ok = ok;

    }

    public TestStruct (SerializationInfo info, StreamingContext context) {
        Ok = info.GetBoolean ("Ok");
    }

    public void GetObjectData (SerializationInfo info, StreamingContext context) {
        info.AddValue ("Ok", Ok);
    }

}

class TestMain {

    [STAThread]
    static void Main(string[] args) {
        TestStruct [] arr = new TestStruct [1];
        arr [0] = new TestStruct (true);

        MemoryStream stream = new MemoryStream();
        IFormatter formatter = new SoapFormatter();
        formatter.Serialize(stream, arr);
        
        stream.Seek (0, SeekOrigin.Begin);

        TestStruct [] readArr = (TestStruct [])formatter.Deserialize(stream);

        stream.Close ();

        if (arr [0].Ok != readArr [0].Ok) {
            Console.WriteLine ("Bug.");
        }
    }
}

This happens on the initial version of the .NET framework, with SP2 installed.

   6:57 PM

Content of this site is © Dejan Jelovic. All rights reserved.