1 //
2 // System.Runtime.Serialization.SerializationTest.cs
3 //
4 // Author: Lluis Sanchez Gual  (lluis@ximian.com)
5 //
6 // (C) Ximian, Inc.
7 //
8 
9 using System;
10 using System.Diagnostics;
11 using System.IO;
12 using System.Runtime.Serialization;
13 using System.Runtime.Serialization.Formatters.Soap;
14 using System.Reflection;
15 using System.Runtime.Remoting;
16 using System.Runtime.Remoting.Channels;
17 using System.Runtime.Remoting.Proxies;
18 using System.Runtime.Remoting.Messaging;
19 using System.Collections;
20 using NUnit.Framework;
21 
22 namespace MonoTests.System.Runtime.Serialization.Formatters.Soap
23 {
24 	[TestFixture]
25 	public class SerializationTest
26 	{
27 		MemoryStream ms;
28 
29 		[Test]
TestSerialization()30 		public void TestSerialization ()
31 		{
32 			MethodTester mt = new MethodTester();
33 			RemotingServices.Marshal (mt, "myuri");
34 
35 			WriteData();
36 			ReadData();
37 
38 			RemotingServices.Disconnect (mt);
39 		}
40 
Main()41 		public static void Main()
42 		{
43 			SerializationTest test = new SerializationTest();
44 			test.TestSerialization();
45 		}
46 
WriteData()47 		void WriteData ()
48 		{
49 			StreamingContext context = new StreamingContext (StreamingContextStates.Other);
50 			SurrogateSelector sel = new SurrogateSelector();
51 			sel.AddSurrogate (typeof (Point), context, new PointSurrogate());
52 
53 			List list = CreateTestData();
54 			BinderTester_A bta = CreateBinderTestData();
55 
56 			ms = new MemoryStream();
57 			SoapFormatter f = new SoapFormatter (sel, new StreamingContext(StreamingContextStates.Other));
58 			f.Serialize (ms, list);
59 //			ProcessMessages (ms, null);
60 //			f.Serialize (ms, bta);
61 			ms.Flush ();
62 			ms.Position = 0;
63 			StreamReader reader = new StreamReader(ms);
64 			Console.WriteLine(reader.ReadToEnd());
65 			ms.Position = 0;
66 		}
67 
ReadData()68 		void ReadData()
69 		{
70 			StreamingContext context = new StreamingContext (StreamingContextStates.Other);
71 			SurrogateSelector sel = new SurrogateSelector();
72 			sel.AddSurrogate (typeof (Point), context, new PointSurrogate());
73 
74 			SoapFormatter f = new SoapFormatter (sel, context);
75 
76 			object list = f.Deserialize (ms);
77 
78 			object[][] originalMsgData = null;
79 			IMessage[] calls = null;
80 			IMessage[] resps = null;
81 
82 //			originalMsgData = ProcessMessages (null, null);
83 
84 //			calls = new IMessage[originalMsgData.Length];
85 //			resps = new IMessage[originalMsgData.Length];
86 
87 
88 //			for (int n=0; n<originalMsgData.Length; n++)
89 //			{
90 //				calls[n] = (IMessage) f.Deserialize (ms);
91 //				resps[n] = (IMessage) f.DeserializeMethodResponse (ms, null, (IMethodCallMessage)calls[n]);
92 //			}
93 //
94 //			f.Binder = new TestBinder ();
95 //			object btbob = f.Deserialize (ms);
96 
97 			ms.Close();
98 
99 			((List)list).CheckEquals(CreateTestData());
100 //
101 //			BinderTester_A bta = CreateBinderTestData();
102 //			Assertion.AssertEquals ("BinderTest.class", btbob.GetType(), typeof (BinderTester_B));
103 //			BinderTester_B btb = btbob as BinderTester_B;
104 //			if (btb != null)
105 //			{
106 //				Assertion.AssertEquals ("BinderTest.x", btb.x, bta.x);
107 //				Assertion.AssertEquals ("BinderTest.y", btb.y, bta.y);
108 //			}
109 //
110 //			CheckMessages ("MethodCall", originalMsgData, ProcessMessages (null, calls));
111 //			CheckMessages ("MethodResponse", originalMsgData, ProcessMessages (null, resps));
112 		}
113 
CreateBinderTestData()114 		BinderTester_A CreateBinderTestData ()
115 		{
116 			BinderTester_A bta = new BinderTester_A();
117 			bta.x = 11;
118 			bta.y = "binder tester";
119 			return bta;
120 		}
121 
CreateTestData()122 		List CreateTestData()
123 		{
124 			List list = new List();
125 			list.name = "my list";
126 			list.values = new SomeValues();
127 			list.values.Init();
128 
129 			ListItem item1 = new ListItem();
130 			ListItem item2 = new ListItem();
131 			ListItem item3 = new ListItem();
132 
133 			item1.label = "value label 1";
134 			item1.next = item2;
135 			item1.value.color = 111;
136 			item1.value.point = new Point();
137 			item1.value.point.x = 11;
138 			item1.value.point.y = 22;
139 
140 			item2.label = "value label 2";
141 			item2.next = item3;
142 			item2.value.color = 222;
143 
144 			item2.value.point = new Point();
145 			item2.value.point.x = 33;
146 			item2.value.point.y = 44;
147 
148 			item3.label = "value label 3";
149 			item3.value.color = 333;
150 			item3.value.point = new Point();
151 			item3.value.point.x = 55;
152 			item3.value.point.y = 66;
153 
154 			list.children = new ListItem[3];
155 
156 			list.children[0] = item1;
157 			list.children[1] = item2;
158 			list.children[2] = item3;
159 
160 			return list;
161 		}
162 
163 
ProcessMessages(Stream stream, IMessage[] messages)164 		object[][] ProcessMessages (Stream stream, IMessage[] messages)
165 		{
166 			object[][] results = new object[9][];
167 
168 			AuxProxy prx = new AuxProxy (stream, "myuri");
169 			MethodTester mt = (MethodTester)prx.GetTransparentProxy();
170 			object res;
171 
172 			if (messages != null) prx.SetTestMessage (messages[0]);
173 			res = mt.OverloadedMethod();
174 			results[0] = new object[] {res};
175 
176 			if (messages != null) prx.SetTestMessage (messages[1]);
177 			res = mt.OverloadedMethod(22);
178 			results[1] = new object[] {res};
179 
180 			if (messages != null) prx.SetTestMessage (messages[2]);
181 			int[] par1 = new int[] {1,2,3};
182 			res = mt.OverloadedMethod(par1);
183 			results[2] = new object[] { res, par1 };
184 
185 			if (messages != null) prx.SetTestMessage (messages[3]);
186 			mt.NoReturn();
187 
188 			if (messages != null) prx.SetTestMessage (messages[4]);
189 			res = mt.Simple ("hello",44);
190 			results[4] = new object[] { res };
191 
192 			if (messages != null) prx.SetTestMessage (messages[5]);
193 			res = mt.Simple2 ('F');
194 			results[5] = new object[] { res };
195 
196 			if (messages != null) prx.SetTestMessage (messages[6]);
197 			char[] par2 = new char[] { 'G' };
198 			res = mt.Simple3 (par2);
199 			results[6] = new object[] { res, par2 };
200 
201 			if (messages != null) prx.SetTestMessage (messages[7]);
202 			res = mt.Simple3 (null);
203 			results[7] = new object[] { res };
204 
205 			if (messages != null) prx.SetTestMessage (messages[8]);
206 
207 			SimpleClass b = new SimpleClass ('H');
208 			res = mt.SomeMethod (123456, b);
209 			results[8] = new object[] { res, b };
210 
211 			return results;
212 		}
213 
CheckMessages(string label, object[][] original, object[][] serialized)214 		void CheckMessages (string label, object[][] original, object[][] serialized)
215 		{
216 			for (int n=0; n<original.Length; n++)
217 				EqualsArray (label + " " + n, original[n], serialized[n]);
218 		}
219 
AssertEquals(string message, Object expected, Object actual)220 		public static void AssertEquals(string message, Object expected, Object actual)
221 		{
222 			if (expected != null && expected.GetType().IsArray)
223 				EqualsArray (message, (Array)expected, (Array)actual);
224 			else
225 				Assert.AreEqual (expected, actual, message);
226 
227 		}
EqualsArray(string message, object oar1, object oar2)228 
229 		public static void EqualsArray (string message, object oar1, object oar2)
230 		{
231 			if (oar1 == null || oar2 == null || !(oar1 is Array) || !(oar2 is Array))
232 			{
233 				SerializationTest.AssertEquals (message, oar1, oar2);
234 				return;
235 			}
236 
237 			Array ar1 = (Array) oar1;
238 			Array ar2 = (Array) oar2;
239 
240 			SerializationTest.AssertEquals(message + ".Length", ar1.Length,ar2.Length);
241 
242 			for (int n=0; n<ar1.Length; n++)
243 			{
244 				object av1 = ar1.GetValue(n);
245 				object av2 = ar2.GetValue(n);
246 				SerializationTest.AssertEquals (message + "[" + n + "]", av1, av2);
247 			}
248 		}
249 	}
250 
251 
252 
253 	class PointSurrogate: ISerializationSurrogate
GetObjectData(object obj, SerializationInfo info, StreamingContext context)254 	{
255 		public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
256 		{
257 			Point p = (Point)obj;
258 			info.AddValue ("xv",p.x);
259 			info.AddValue ("yv",p.y);
260 		}
261 
262 		public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
263 		{
264 			typeof (Point).GetField ("x").SetValue (obj, info.GetInt32 ("xv"));
265 			typeof (Point).GetField ("y").SetValue (obj, info.GetInt32 ("yv"));
266 			return obj;
267 		}
268 	}
269 
270 	[Serializable]
271 	public class List
272 	{
273 		public string name = null;
274 		public ListItem[] children = null;
275 		public SomeValues values;
CheckEquals(List val)276 
277 		public void CheckEquals(List val)
278 		{
279 			SerializationTest.AssertEquals ("List.children.Length", children.Length, val.children.Length);
280 
281 			for (int n=0; n<children.Length; n++)
282 				children[n].CheckEquals (val.children[n]);
283 
284 			SerializationTest.AssertEquals ("List.name", name, val.name);
285 			values.CheckEquals (val.values);
286 		}
287 	}
288 
289 	[Serializable]
290 	public class ListItem: ISerializable
ListItem()291 	{
292 		public ListItem()
293 		{
294 		}
295 
296 		ListItem (SerializationInfo info, StreamingContext ctx)
297 		{
298 			next = (ListItem)info.GetValue ("next", typeof (ListItem));
299 			value = (ListValue)info.GetValue ("value", typeof (ListValue));
300 			label = info.GetString ("label");
301 		}
302 
303 		public void GetObjectData (SerializationInfo info, StreamingContext ctx)
304 		{
305 			info.AddValue ("next", next);
306 			info.AddValue ("value", value);
307 			info.AddValue ("label", label);
308 		}
309 
310 		public void CheckEquals(ListItem val)
311 		{
312 			SerializationTest.AssertEquals ("ListItem.next", next, val.next);
313 			SerializationTest.AssertEquals ("ListItem.label", label, val.label);
314 			value.CheckEquals (val.value);
315 		}
316 
317 		public override bool Equals(object obj)
318 		{
319 			ListItem val = (ListItem)obj;
320 			if ((next == null || val.next == null) && (next != val.next)) return false;
321 			if (next == null) return true;
322 			if (!next.Equals(val.next)) return false;
323 			return value.Equals (val.value) && label == val.label;
324 		}
325 
326 		public override int GetHashCode ()
327 		{
328 			return base.GetHashCode ();
329 		}
330 
331 		public ListItem next;
332 		public ListValue value;
333 		public string label;
334 	}
335 
336 	[Serializable]
337 	public struct ListValue
338 	{
339 		public int color;
340 		public Point point;
EqualsMonoTests.System.Runtime.Serialization.Formatters.Soap.ListValue341 
342 		public override bool Equals(object obj)
343 		{
344 			ListValue val = (ListValue)obj;
345 			return (color == val.color && point.Equals(val.point));
346 		}
CheckEqualsMonoTests.System.Runtime.Serialization.Formatters.Soap.ListValue347 
348 		public void CheckEquals(ListValue val)
349 		{
350 			SerializationTest.AssertEquals ("ListValue.color", color, val.color);
351 			point.CheckEquals (val.point);
352 		}
GetHashCodeMonoTests.System.Runtime.Serialization.Formatters.Soap.ListValue353 
354 		public override int GetHashCode ()
355 		{
356 			return base.GetHashCode ();
357 		}
358 	}
359 
360 //	[Serializable]
361 	public struct Point
362 	{
363 		public int x;
364 		public int y;
EqualsMonoTests.System.Runtime.Serialization.Formatters.Soap.Point365 
366 		public override bool Equals(object obj)
367 		{
368 			Point p = (Point)obj;
369 			return (x == p.x && y == p.y);
370 		}
CheckEqualsMonoTests.System.Runtime.Serialization.Formatters.Soap.Point371 
372 		public void CheckEquals(Point p)
373 		{
374 			SerializationTest.AssertEquals ("Point.x", x, p.x);
375 			SerializationTest.AssertEquals ("Point.y", y, p.y);
376 		}
GetHashCodeMonoTests.System.Runtime.Serialization.Formatters.Soap.Point377 
378 		public override int GetHashCode ()
379 		{
380 			return base.GetHashCode ();
381 		}
382 	}
383 
384 	[Serializable]
385 	public class SimpleClass
SimpleClass(char v)386 	{
387 		public SimpleClass (char v) { val = v; }
388 
389 		public override bool Equals(object obj)
390 		{
391 			if (obj == null) return false;
392 			return val == ((SimpleClass)obj).val;
393 		}
394 
395 		public override int GetHashCode()
396 		{
397 			return val.GetHashCode();
398 		}
399 
400 		public int SampleCall (string str, SomeValues sv, ref int acum)
401 		{
402 			acum += (int)val;
403 			return (int)val;
404 		}
405 
406 		public char val;
407 	}
408 
409 	enum IntEnum { aaa, bbb, ccc }
410 	enum ByteEnum: byte { aaa=221, bbb=3, ccc=44 }
SampleDelegate(string str, SomeValues sv, ref int acum)411 
412 	delegate int SampleDelegate (string str, SomeValues sv, ref int acum);
413 
414 	[Serializable]
415 	public class SomeValues
416 	{
417 		Type _type;
418 		Type _type2;
419 		DBNull _dbnull;
420 		Assembly _assembly;
421 		IntEnum _intEnum;
422 		ByteEnum _byteEnum;
423 
424 		bool _bool;
425 		bool _bool2;
426 		byte _byte;
427 		char _char;
428 //		DateTime _dateTime;
429 		decimal _decimal;
430 		double _double;
431 		short _short;
432 		int _int;
433 		long _long;
434 		sbyte _sbyte;
435 		float _float;
436 		ushort _ushort;
437 		uint _uint;
438 		ulong _ulong;
439 
440 		object[] _objects;
441 		string[] _strings;
442 		int[] _ints;
443 		public int[,,] _intsMulti;
444 		int[][] _intsJagged;
445 		SimpleClass[] _simples;
446 		SimpleClass[,] _simplesMulti;
447 		SimpleClass[][] _simplesJagged;
448 		double[] _doubles;
449 		object[] _almostEmpty;
450 
451 		object[] _emptyObjectArray;
452 		Type[] _emptyTypeArray;
453 		SimpleClass[] _emptySimpleArray;
454 		int[] _emptyIntArray;
455 		string[] _emptyStringArray;
456 
457 
458 		SampleDelegate _sampleDelegate;
459 		SampleDelegate _sampleDelegate2;
460 		SampleDelegate _sampleDelegate3;
461 		SampleDelegate _sampleDelegateStatic;
462 		SampleDelegate _sampleDelegateCombined;
463 
464 		SimpleClass _shared1;
465 		SimpleClass _shared2;
466 		SimpleClass _shared3;
Init()467 
468 		public void Init()
469 		{
470 			_type = typeof (string);
471 			_type2 = typeof (SomeValues);
472 			_dbnull = DBNull.Value;
473 			_assembly = typeof (SomeValues).Assembly;
474 			_intEnum = IntEnum.bbb;
475 			_byteEnum = ByteEnum.ccc;
476 			_bool = true;
477 			_bool2 = false;
478 			_byte = 254;
479 			_char = 'A';
480 //			_dateTime = new DateTime (1972,7,13,1,20,59);
481 			_decimal = (decimal)101010.10101;
482 			_double = 123456.6789;
483 			_short = -19191;
484 			_int = -28282828;
485 			_long = 37373737373;
486 			_sbyte = -123;
487 			_float = (float)654321.321;
488 			_ushort = 61616;
489 			_uint = 464646464;
490 			_ulong = 55555555;
491 
492 			Point p = new Point();
493 			p.x = 56; p.y = 67;
494 			object boxedPoint = p;
495 
496 			long i = 22;
497 			object boxedLong = i;
498 
499 			_objects = new object[] { "string", (int)1234, null , /*boxedPoint, boxedPoint,*/ boxedLong, boxedLong};
500 			_strings = new string[] { "an", "array", "of", "strings","I","repeat","an", "array", "of", "strings" };
501 			_ints = new int[] { 4,5,6,7,8 };
502 			_intsMulti = new int[2,3,4] { { {1,2,3,4},{5,6,7,8},{9,10,11,12}}, { {13,14,15,16},{17,18,19,20},{21,22,23,24} } };
503 			_intsJagged = new int[2][] { new int[3] {1,2,3}, new int[2] {4,5} };
504 			_simples = new SimpleClass[] { new SimpleClass('a'),new SimpleClass('b'),new SimpleClass('c') };
505 			_simplesMulti = new SimpleClass[2,3] {{new SimpleClass('d'),new SimpleClass('e'),new SimpleClass('f')}, {new SimpleClass('g'),new SimpleClass('j'),new SimpleClass('h')}};
506 			_simplesJagged = new SimpleClass[2][] { new SimpleClass[1] { new SimpleClass('i') }, new SimpleClass[2] {null, new SimpleClass('k')}};
507 			_almostEmpty = new object[2000];
508 			_almostEmpty[1000] = 4;
509 
510 			_emptyObjectArray = new object[0];
511 			_emptyTypeArray = new Type[0];
512 			_emptySimpleArray = new SimpleClass[0];
513 			_emptyIntArray = new int[0];
514 			_emptyStringArray = new string[0];
515 
516 			// FIXME: Once double.ToString("G17") is implemented
517 			// we'll be able to serialize double.MaxValue and double.MinValue.
518 			// Currently, it throws a System.OverflowException.
519 			//_doubles = new double[] { 1010101.101010, 292929.29292, 3838383.38383, 4747474.474, 56565.5656565, 0, Double.NaN, Double.MaxValue, Double.MinValue, Double.NegativeInfinity, Double.PositiveInfinity };
520 			_doubles = new double[] { 1010101.101010, 292929.29292, 3838383.38383, 4747474.474, 56565.5656565, 0, Double.NaN, Double.NegativeInfinity, Double.PositiveInfinity };
521 
522 			_sampleDelegate = new SampleDelegate(SampleCall);
523 			_sampleDelegate2 = new SampleDelegate(_simples[0].SampleCall);
524 			_sampleDelegate3 = new SampleDelegate(new SimpleClass('x').SampleCall);
525 			_sampleDelegateStatic = new SampleDelegate(SampleStaticCall);
526 			_sampleDelegateCombined = (SampleDelegate)Delegate.Combine (new Delegate[] {_sampleDelegate, _sampleDelegate2, _sampleDelegate3, _sampleDelegateStatic });
527 
528 			// This is to test that references are correctly solved
529 			_shared1 = new SimpleClass('A');
530 			_shared2 = new SimpleClass('A');
531 			_shared3 = _shared1;
532 		}
SampleCall(string str, SomeValues sv, ref int acum)533 
534 		public int SampleCall (string str, SomeValues sv, ref int acum)
535 		{
536 			acum += _int;
537 			return _int;
538 		}
SampleStaticCall(string str, SomeValues sv, ref int acum)539 
540 		public static int SampleStaticCall (string str, SomeValues sv, ref int acum)
541 		{
542 			acum += 99;
543 			return 99;
544 		}
CheckEquals(SomeValues obj)545 
546 		public void CheckEquals(SomeValues obj)
547 		{
548 			SerializationTest.AssertEquals ("SomeValues._type", _type, obj._type);
549 			SerializationTest.AssertEquals ("SomeValues._type2", _type2, obj._type2);
550 			SerializationTest.AssertEquals ("SomeValues._dbnull", _dbnull, obj._dbnull);
551 			SerializationTest.AssertEquals ("SomeValues._assembly", _assembly, obj._assembly);
552 
553 			SerializationTest.AssertEquals ("SomeValues._intEnum", _intEnum, obj._intEnum);
554 			SerializationTest.AssertEquals ("SomeValues._byteEnum", _byteEnum, obj._byteEnum);
555 			SerializationTest.AssertEquals ("SomeValues._bool", _bool, obj._bool);
556 			SerializationTest.AssertEquals ("SomeValues._bool2", _bool2, obj._bool2);
557 			SerializationTest.AssertEquals ("SomeValues._byte", _byte, obj._byte);
558 			SerializationTest.AssertEquals ("SomeValues._char", _char, obj._char);
559 //			SerializationTest.AssertEquals ("SomeValues._dateTime", _dateTime, obj._dateTime);
560 			SerializationTest.AssertEquals ("SomeValues._decimal", _decimal, obj._decimal);
561 			SerializationTest.AssertEquals ("SomeValues._int", _int, obj._int);
562 			SerializationTest.AssertEquals ("SomeValues._long", _long, obj._long);
563 			SerializationTest.AssertEquals ("SomeValues._sbyte", _sbyte, obj._sbyte);
564 			SerializationTest.AssertEquals ("SomeValues._float", _float, obj._float);
565 			SerializationTest.AssertEquals ("SomeValues._ushort", _ushort, obj._ushort);
566 			SerializationTest.AssertEquals ("SomeValues._uint", _uint, obj._uint);
567 			SerializationTest.AssertEquals ("SomeValues._ulong", _ulong, obj._ulong);
568 
569 			SerializationTest.EqualsArray ("SomeValues._objects", _objects, obj._objects);
570 			SerializationTest.EqualsArray ("SomeValues._strings", _strings, obj._strings);
571 			SerializationTest.EqualsArray ("SomeValues._doubles", _doubles, obj._doubles);
572 			SerializationTest.EqualsArray ("SomeValues._ints", _ints, obj._ints);
573 			SerializationTest.EqualsArray ("SomeValues._simples", _simples, obj._simples);
574 			SerializationTest.EqualsArray ("SomeValues._almostEmpty", _almostEmpty, obj._almostEmpty);
575 
576 			SerializationTest.EqualsArray ("SomeValues._emptyObjectArray", _emptyObjectArray, obj._emptyObjectArray);
577 			SerializationTest.EqualsArray ("SomeValues._emptyTypeArray", _emptyTypeArray, obj._emptyTypeArray);
578 			SerializationTest.EqualsArray ("SomeValues._emptySimpleArray", _emptySimpleArray, obj._emptySimpleArray);
579 			SerializationTest.EqualsArray ("SomeValues._emptyIntArray", _emptyIntArray, obj._emptyIntArray);
580 			SerializationTest.EqualsArray ("SomeValues._emptyStringArray", _emptyStringArray, obj._emptyStringArray);
581 
582 			for (int i=0; i<2; i++)
583 				for (int j=0; j<3; j++)
584 					for (int k=0; k<4; k++)
585 						SerializationTest.AssertEquals("SomeValues._intsMulti[" + i + "," + j + "," + k + "]", _intsMulti[i,j,k], obj._intsMulti[i,j,k]);
586 
587 			for (int i=0; i<_intsJagged.Length; i++)
588 				for (int j=0; j<_intsJagged[i].Length; j++)
589 					SerializationTest.AssertEquals ("SomeValues._intsJagged[" + i + "][" + j + "]", _intsJagged[i][j], obj._intsJagged[i][j]);
590 
591 			for (int i=0; i<2; i++)
592 				for (int j=0; j<3; j++)
593 					SerializationTest.AssertEquals ("SomeValues._simplesMulti[" + i + "," + j + "]", _simplesMulti[i,j], obj._simplesMulti[i,j]);
594 
595 			for (int i=0; i<_simplesJagged.Length; i++)
596 				SerializationTest.EqualsArray ("SomeValues._simplesJagged", _simplesJagged[i], obj._simplesJagged[i]);
597 
598 			int acum = 0;
599 			SerializationTest.AssertEquals ("SomeValues._sampleDelegate", _sampleDelegate ("hi", this, ref acum), _int);
600 			SerializationTest.AssertEquals ("SomeValues._sampleDelegate_bis", _sampleDelegate ("hi", this, ref acum), obj._sampleDelegate ("hi", this, ref acum));
601 
602 			SerializationTest.AssertEquals ("SomeValues._sampleDelegate2", _sampleDelegate2 ("hi", this, ref acum), (int)_simples[0].val);
603 			SerializationTest.AssertEquals ("SomeValues._sampleDelegate2_bis", _sampleDelegate2 ("hi", this, ref acum), obj._sampleDelegate2 ("hi", this, ref acum));
604 
605 			SerializationTest.AssertEquals ("SomeValues._sampleDelegate3", _sampleDelegate3 ("hi", this, ref acum), (int)'x');
606 			SerializationTest.AssertEquals ("SomeValues._sampleDelegate3_bis", _sampleDelegate3 ("hi", this, ref acum), obj._sampleDelegate3 ("hi", this, ref acum));
607 
608 			SerializationTest.AssertEquals ("SomeValues._sampleDelegateStatic", _sampleDelegateStatic ("hi", this, ref acum), 99);
609 			SerializationTest.AssertEquals ("SomeValues._sampleDelegateStatic_bis", _sampleDelegateStatic ("hi", this, ref acum), obj._sampleDelegateStatic ("hi", this, ref acum));
610 
611 			int acum1 = 0;
612 			int acum2 = 0;
613 			_sampleDelegateCombined ("hi", this, ref acum1);
614 			obj._sampleDelegateCombined ("hi", this, ref acum2);
615 
616 			SerializationTest.AssertEquals ("_sampleDelegateCombined", acum1, _int + (int)_simples[0].val + (int)'x' + 99);
617 			SerializationTest.AssertEquals ("_sampleDelegateCombined_bis", acum1, acum2);
618 
619 			SerializationTest.AssertEquals ("SomeValues._shared1", _shared1, _shared2);
620 			SerializationTest.AssertEquals ("SomeValues._shared1_bis", _shared1, _shared3);
621 
622 			_shared1.val = 'B';
623 			SerializationTest.AssertEquals ("SomeValues._shared2", _shared2.val, 'A');
624 			SerializationTest.AssertEquals ("SomeValues._shared3", _shared3.val, 'B');
625 		}
626 	}
627 
628 	class MethodTester : MarshalByRefObject
OverloadedMethod()629 	{
630 		public int OverloadedMethod ()
631 		{
632 			return 123456789;
633 		}
634 
635 		public int OverloadedMethod (int a)
636 		{
637 			return a+2;
638 		}
639 
640 		public int OverloadedMethod (int[] a)
641 		{
642 			return a.Length;
643 		}
644 
645 		public void NoReturn ()
646 		{}
647 
648 		public string Simple (string a, int b)
649 		{
650 			return a + b;
651 		}
652 
653 		public SimpleClass Simple2 (char c)
654 		{
655 			return new SimpleClass(c);
656 		}
657 
658 		public SimpleClass Simple3 (char[] c)
659 		{
660 			if (c != null) return new SimpleClass(c[0]);
661 			else return null;
662 		}
663 
664 		public int SomeMethod (int a, SimpleClass b)
665 		{
666 			object[] d;
667 			string c = "hi";
668 			int r = a + c.Length;
669 			c = "bye";
670 			d = new object[3];
671 			d[1] = b;
672 			return r;
673 		}
674 	}
675 
676 	class AuxProxy: RealProxy
677 	{
678 		public static bool useHeaders = false;
679 		Stream _stream;
680 		string _uri;
681 		IMethodMessage _testMsg;
AuxProxy(Stream stream, string uri)682 
683 		public AuxProxy(Stream stream, string uri): base(typeof(MethodTester))
684 		{
685 			_stream = stream;
686 			_uri = uri;
687 		}
SetTestMessage(IMessage msg)688 
689 		public void SetTestMessage (IMessage msg)
690 		{
691 			_testMsg = (IMethodMessage)msg;
692 			_testMsg.Properties["__Uri"] = _uri;
693 		}
Invoke(IMessage msg)694 
695 		public override IMessage Invoke(IMessage msg)
696 		{
697 			IMethodCallMessage call = (IMethodCallMessage)msg;
698 			if (call.MethodName.StartsWith ("Initialize")) return new ReturnMessage(null,null,0,null,(IMethodCallMessage)msg);
699 
700 			call.Properties["__Uri"] = _uri;
701 
702 			if (_stream != null)
703 			{
704 				SerializeCall (call);
705 				IMessage response = ChannelServices.SyncDispatchMessage (call);
706 				SerializeResponse (response);
707 				return response;
708 			}
709 			else if (_testMsg != null)
710 			{
711 				if (_testMsg is IMethodCallMessage)
712 					return ChannelServices.SyncDispatchMessage (_testMsg);
713 				else
714 					return _testMsg;
715 			}
716 			else
717 				return ChannelServices.SyncDispatchMessage (call);
718 		}
SerializeCall(IMessage call)719 
720 		void SerializeCall (IMessage call)
721 		{
722 			RemotingSurrogateSelector rss = new RemotingSurrogateSelector();
723 			IRemotingFormatter fmt = new SoapFormatter (rss, new StreamingContext(StreamingContextStates.Remoting));
724 			fmt.Serialize (_stream, call, GetHeaders());
725 		}
SerializeResponse(IMessage resp)726 
727 		void SerializeResponse (IMessage resp)
728 		{
729 			RemotingSurrogateSelector rss = new RemotingSurrogateSelector();
730 			IRemotingFormatter fmt = new SoapFormatter (rss, new StreamingContext(StreamingContextStates.Remoting));
731 			fmt.Serialize (_stream, resp, GetHeaders());
732 		}
GetHeaders()733 
734 		Header[] GetHeaders()
735 		{
736 			Header[] hs = null;
737 			if (useHeaders)
738 			{
739 				hs = new Header[1];
740 				hs[0] = new Header("unom",new SimpleClass('R'));
741 			}
742 			return hs;
743 		}
744 	}
745 
746 	public class TestBinder : SerializationBinder
BindToType(string assemblyName, string typeName)747 	{
748 		public override Type BindToType (string assemblyName, string typeName)
749 		{
750 			if (typeName.IndexOf("BinderTester_A") != -1)
751 				typeName = typeName.Replace ("BinderTester_A", "BinderTester_B");
752 
753 			return Assembly.Load (assemblyName).GetType (typeName);
754 		}
755 	}
756 
757 	[Serializable]
758 	public class BinderTester_A
759 	{
760 		public int x;
761 		public string y;
762 	}
763 
764 	[Serializable]
765 	public class BinderTester_B
766 	{
767 		public string y;
768 		public int x;
769 	}
770 
771 
772 }
773