private static void SingletonSerializationTest() { Singleton[] a1 = { Singleton.GetSingleton(), Singleton.GetSingleton() }; Console.WriteLine("Do both elements refer to the same object?{0}", a1[0] == a1[1]); using (var stream = new MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, a1); stream.Position = 0; Singleton[] a2 = (Singleton[])formatter.Deserialize(stream); Console.WriteLine("Do both elements refer to the same object?{0}", a2[0] == a2[1]); Console.WriteLine("Do all elements refer to the same object?{0}", a1[0] == a2[0]); } }
//这个方法在对象反序列之后调用 public object GetRealObject(StreamingContext context) { return(Singleton.GetSingleton()); }