PageRenderTime 61ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/System.Runtime.Serialization.Formatters.Soap/Test/SerializationTest.cs

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