[Mono-bugs] [Bug 70104][Nor] Changed - Serialization/deserialization issues

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Wed, 8 Dec 2004 15:09:30 -0500 (EST)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by shurik_u@freemail.ru.

http://bugzilla.ximian.com/show_bug.cgi?id=70104

--- shadow/70104	2004-12-07 12:45:37.000000000 -0500
+++ shadow/70104.tmp.5705	2004-12-08 15:09:30.000000000 -0500
@@ -54,6 +54,85 @@
 
 This behavior is typical both for Mono 1.0 and Mono 1.1
 
 ------- Additional Comments From lluis@ximian.com  2004-12-07 12:45 -------
 Can you provide a standalone test case that shows the issue? it would
 be of great help. Thanks.
+
+------- Additional Comments From shurik_u@freemail.ru  2004-12-08 15:09 -------
+using System;
+using System.IO;
+using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters;
+using System.Runtime.Serialization.Formatters.Binary;
+
+namespace SerializeTest
+{
+  [Serializable]
+  public class B : ISerializable
+  {
+    public int field = 2;
+
+    public void GetObjectData(SerializationInfo info, 
+StreamingContext context)
+    {
+      throw new InvalidOperationException("Serialize:We should not 
+pass here.");
+    }
+
+    public B()
+    {
+    }
+    //Deserialization constructor
+    public B (SerializationInfo info, StreamingContext context)
+    {
+      throw new InvalidOperationException("Deserialize:We should not 
+pass here.");
+    }
+  }
+
+  public class BSerializationSurrogate : ISerializationSurrogate
+  {
+    public void GetObjectData(object obj, SerializationInfo info, 
+StreamingContext context)
+    {
+      Console.WriteLine("Serialize");        
+      info.AddValue("field", Convert.ToString(((B)obj).field));
+    }
+
+    public object SetObjectData(object obj, SerializationInfo info, 
+StreamingContext context, ISurrogateSelector selector)
+    {
+      Console.WriteLine("Deserialize");        
+      ((B)obj).field = Convert.ToInt32(info.GetValue("field", typeof
+(string)));
+      return obj;
+    }
+  }
+  
+  /// <summary>
+  /// Summary description for Class1.
+  /// </summary>
+  class SerializationDeserializationTest
+  {
+    /// <summary>
+    /// The main entry point for the application.
+    /// </summary>
+    [STAThread]
+    static void Main(string[] args)
+    {
+      B instance = new B();
+      MemoryStream ms = new MemoryStream();
+      BinaryFormatter bf = new BinaryFormatter();
+      SurrogateSelector ss = new SurrogateSelector();
+      ss.AddSurrogate(typeof(B), new StreamingContext
+(StreamingContextStates.All), new BSerializationSurrogate());
+      bf.SurrogateSelector = ss;
+      bf.Serialize(ms, instance);
+      ms.Position=0;
+      B newInstance = (B)bf.Deserialize(ms);
+      Console.WriteLine("Succes!");
+      Console.ReadLine();
+    }
+  }
+}
+