ObjectManagerTest.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // System.Runtime.Serialization.ObjectManagerTest.cs
  3. //
  4. // Author: Martin Baulig ([email protected])
  5. //
  6. // (C) Novell
  7. //
  8. using System;
  9. using System.IO;
  10. using System.Text;
  11. using System.Runtime.Serialization;
  12. using System.Runtime.Serialization.Formatters.Binary;
  13. using NUnit.Framework;
  14. namespace MonoTests.System.Runtime.Serialization
  15. {
  16. [TestFixture]
  17. public class ObjectManagerTest
  18. {
  19. [Test] // bug 76931
  20. public void TestSerialization ()
  21. {
  22. using (MemoryStream ms = new MemoryStream ()) {
  23. Bar bar = new Bar (8, 3, 5, 21);
  24. bar.Save (ms);
  25. ms.Position = 0;
  26. bar = Bar.Load (ms);
  27. Assert.AreEqual ("Bar [Foo (16),(Foo (6),Foo (10),Foo (42)]",
  28. bar.ToString (), "#1");
  29. }
  30. }
  31. }
  32. public class Foo
  33. {
  34. public int Data;
  35. public Foo (int data)
  36. {
  37. this.Data = data;
  38. }
  39. public override string ToString ()
  40. {
  41. return String.Format ("Foo ({0})", Data);
  42. }
  43. internal class SerializationSurrogate : ISerializationSurrogate
  44. {
  45. public void GetObjectData (object obj, SerializationInfo info, StreamingContext context)
  46. {
  47. Foo foo = (Foo) obj;
  48. info.AddValue ("data", foo.Data);
  49. }
  50. public object SetObjectData (object obj, SerializationInfo info,
  51. StreamingContext context,
  52. ISurrogateSelector selector)
  53. {
  54. Foo foo = (Foo) obj;
  55. foo.Data = info.GetInt32 ("data");
  56. return new Foo (2 * foo.Data);
  57. }
  58. }
  59. }
  60. [Serializable]
  61. public class Bar
  62. {
  63. public readonly Foo Foo;
  64. public readonly Foo[] Array;
  65. public Bar (int a, params int[] b)
  66. {
  67. Foo = new Foo (a);
  68. Array = new Foo[b.Length];
  69. for (int i = 0; i < b.Length; i++)
  70. Array[i] = new Foo (b[i]);
  71. }
  72. public void Save (Stream stream)
  73. {
  74. SurrogateSelector ss = new SurrogateSelector ();
  75. StreamingContext context = new StreamingContext (
  76. StreamingContextStates.Persistence, this);
  77. ss.AddSurrogate (typeof (Foo), context, new Foo.SerializationSurrogate ());
  78. BinaryFormatter formatter = new BinaryFormatter (ss, context);
  79. formatter.Serialize (stream, this);
  80. }
  81. public static Bar Load (Stream stream)
  82. {
  83. SurrogateSelector ss = new SurrogateSelector ();
  84. StreamingContext context = new StreamingContext (
  85. StreamingContextStates.Persistence, null);
  86. ss.AddSurrogate (typeof (Foo), context, new Foo.SerializationSurrogate ());
  87. BinaryFormatter formatter = new BinaryFormatter (ss, context);
  88. return (Bar) formatter.Deserialize (stream);
  89. }
  90. public override string ToString ()
  91. {
  92. StringBuilder sb = new StringBuilder ();
  93. sb.Append ("Bar [");
  94. sb.Append (Foo);
  95. sb.Append (",(");
  96. for (int i = 0; i < Array.Length; i++) {
  97. if (i > 0)
  98. sb.Append (",");
  99. sb.Append (Array[i]);
  100. }
  101. sb.Append ("]");
  102. return sb.ToString ();
  103. }
  104. }
  105. }