// // System.Runtime.Serialization.SerializationTest.cs // // Author: Lluis Sanchez Gual (lluis@ximian.com) // // (C) Ximian, Inc. // using System; using System.Diagnostics; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Reflection; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Proxies; using System.Runtime.Remoting.Messaging; using System.Collections; using NUnit.Framework; using System.Text; namespace MonoTests.System.Runtime.Serialization { [TestFixture] public class SerializationTest { MemoryStream ms; string uri; #if FEATURE_REMOTING [Test] [Category ("NotWorkingRuntimeInterpreter")] public void TestSerialization () { MethodTester mt = new MethodTester(); RemotingServices.Marshal (mt); uri = RemotingServices.GetObjectUri (mt); WriteData(); ReadData(); RemotingServices.Disconnect (mt); } #endif #if !MONOTOUCH && !FULL_AOT_RUNTIME [Test] public void DelegateSerializationTest () { var a = new DelegateSerialization (); a.E += HandleE1; var d2 = Delegate.CreateDelegate (typeof(Func), "val", typeof(SerializationTest).GetMethod ("HandleE2")); a.E += (Func) d2; using (var ms = new MemoryStream ()) { var fmt = new BinaryFormatter (); fmt.Serialize (ms, a); ms.Flush (); ms.Seek (0, SeekOrigin.Begin); var a2 = (DelegateSerialization) fmt.Deserialize (ms); a2.Test (); } } #endif static int HandleE1 (StringBuilder arg) { arg.Append ("E1"); return 1; } public static int HandleE2 (object o, StringBuilder arg) { arg.Append ("E2|"); arg.Append (o); return 2; } #if FEATURE_REMOTING void WriteData () { StreamingContext context = new StreamingContext (StreamingContextStates.Other); SurrogateSelector sel = new SurrogateSelector(); sel.AddSurrogate (typeof (Point), context, new PointSurrogate()); sel.AddSurrogate (typeof (FalseISerializable), context, new FalseISerializableSurrogate()); List list = CreateTestData(); BinderTester_A bta = CreateBinderTestData(); ms = new MemoryStream(); BinaryFormatter f = new BinaryFormatter (sel, new StreamingContext(StreamingContextStates.Other)); f.Serialize (ms, list); ProcessMessages (ms, null); f.Serialize (ms, bta); ms.Flush (); ms.Position = 0; } void ReadData() { StreamingContext context = new StreamingContext (StreamingContextStates.Other); SurrogateSelector sel = new SurrogateSelector(); sel.AddSurrogate (typeof (Point), context, new PointSurrogate()); sel.AddSurrogate (typeof (FalseISerializable), context, new FalseISerializableSurrogate()); BinaryFormatter f = new BinaryFormatter (sel, context); object list = f.Deserialize (ms); object[][] originalMsgData = null; IMessage[] calls = null; IMessage[] resps = null; originalMsgData = ProcessMessages (null, null); calls = new IMessage[originalMsgData.Length]; resps = new IMessage[originalMsgData.Length]; for (int n=0; n E; public void Test () { var sb = new StringBuilder (); Assert.AreEqual (2, E (sb), "#1"); Assert.AreEqual ("E1E2|val", sb.ToString (), "#2"); } } }