PageRenderTime 54ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/System.Runtime.Serialization/Test/System.Runtime.Serialization/XmlObjectSerializerTest.cs

https://bitbucket.org/danipen/mono
C# | 2223 lines | 1869 code | 276 blank | 78 comment | 0 complexity | 41cef6c9c3f0ec7c46ceb5bb6e3e545d 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. // XmlObjectSerializerTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <atsushi@ximian.com>
  6. // Ankit Jain <JAnkit@novell.com>
  7. //
  8. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. //
  30. // This test code contains tests for both DataContractSerializer and
  31. // NetDataContractSerializer. The code could be mostly common.
  32. //
  33. using System;
  34. using System.Collections;
  35. using System.Collections.Generic;
  36. using System.Collections.ObjectModel;
  37. using System.Data;
  38. using System.IO;
  39. using System.Linq;
  40. using System.Net;
  41. using System.Runtime.Serialization;
  42. using System.Text;
  43. using System.Xml;
  44. using System.Xml.Schema;
  45. using System.Xml.Serialization;
  46. using NUnit.Framework;
  47. [assembly: ContractNamespace ("http://www.u2u.be/samples/wcf/2009", ClrNamespace = "U2U.DataContracts")] // bug #599889
  48. namespace MonoTests.System.Runtime.Serialization
  49. {
  50. [TestFixture]
  51. public class DataContractSerializerTest
  52. {
  53. static readonly XmlWriterSettings settings;
  54. static DataContractSerializerTest ()
  55. {
  56. settings = new XmlWriterSettings ();
  57. settings.OmitXmlDeclaration = true;
  58. }
  59. [DataContract]
  60. class Sample1
  61. {
  62. [DataMember]
  63. public string Member1;
  64. }
  65. [Test]
  66. [ExpectedException (typeof (ArgumentNullException))]
  67. public void ConstructorTypeNull ()
  68. {
  69. new DataContractSerializer (null);
  70. }
  71. [Test]
  72. public void ConstructorKnownTypesNull ()
  73. {
  74. // null knownTypes is allowed. Though the property is filled.
  75. Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), null).KnownTypes, "#1");
  76. Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), "Foo", String.Empty, null).KnownTypes, "#2");
  77. Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), new XmlDictionary ().Add ("Foo"), XmlDictionaryString.Empty, null).KnownTypes, "#3");
  78. }
  79. [Test]
  80. [ExpectedException (typeof (ArgumentNullException))]
  81. public void ConstructorNameNull ()
  82. {
  83. new DataContractSerializer (typeof (Sample1), null, String.Empty);
  84. }
  85. [Test]
  86. [ExpectedException (typeof (ArgumentNullException))]
  87. public void ConstructorNamespaceNull ()
  88. {
  89. new DataContractSerializer (typeof (Sample1), "foo", null);
  90. }
  91. [Test]
  92. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  93. public void ConstructorNegativeMaxObjects ()
  94. {
  95. new DataContractSerializer (typeof (Sample1), null,
  96. -1, false, false, null);
  97. }
  98. [Test]
  99. public void ConstructorMisc ()
  100. {
  101. new DataContractSerializer (typeof (GlobalSample1));
  102. }
  103. [Test]
  104. public void WriteObjectContent ()
  105. {
  106. StringWriter sw = new StringWriter ();
  107. using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
  108. DataContractSerializer ser =
  109. new DataContractSerializer (typeof (string));
  110. xw.WriteStartElement ("my-element");
  111. ser.WriteObjectContent (xw, "TEST STRING");
  112. xw.WriteEndElement ();
  113. }
  114. Assert.AreEqual ("<my-element>TEST STRING</my-element>",
  115. sw.ToString ());
  116. }
  117. [Test]
  118. public void WriteObjectToStream ()
  119. {
  120. DataContractSerializer ser =
  121. new DataContractSerializer (typeof (int));
  122. MemoryStream sw = new MemoryStream ();
  123. ser.WriteObject (sw, 1);
  124. string expected = "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>";
  125. byte[] buf = sw.ToArray ();
  126. Assert.AreEqual (expected, Encoding.UTF8.GetString (buf, 0, buf.Length));
  127. }
  128. [Test]
  129. public void ReadObjectFromStream ()
  130. {
  131. DataContractSerializer ser =
  132. new DataContractSerializer (typeof (int));
  133. string expected = "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>";
  134. byte[] buf = Encoding.UTF8.GetBytes (expected);
  135. MemoryStream sw = new MemoryStream (buf);
  136. object res = ser.ReadObject (sw);
  137. Assert.AreEqual (1, res);
  138. }
  139. // int
  140. [Test]
  141. public void SerializeInt ()
  142. {
  143. DataContractSerializer ser =
  144. new DataContractSerializer (typeof (int));
  145. SerializeInt (ser, "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>");
  146. }
  147. [Test]
  148. [Category ("NotWorking")]
  149. public void NetSerializeInt ()
  150. {
  151. NetDataContractSerializer ser =
  152. new NetDataContractSerializer ();
  153. // z:Assembly="0" ???
  154. SerializeInt (ser, String.Format ("<int z:Type=\"System.Int32\" z:Assembly=\"0\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\" xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>", typeof (int).Assembly.FullName));
  155. }
  156. void SerializeInt (XmlObjectSerializer ser, string expected)
  157. {
  158. StringWriter sw = new StringWriter ();
  159. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  160. ser.WriteObject (w, 1);
  161. }
  162. Assert.AreEqual (expected, sw.ToString ());
  163. }
  164. // pass typeof(DCEmpty), serialize int
  165. [Test]
  166. public void SerializeIntForDCEmpty ()
  167. {
  168. DataContractSerializer ser =
  169. new DataContractSerializer (typeof (DCEmpty));
  170. // tricky!
  171. SerializeIntForDCEmpty (ser, "<DCEmpty xmlns:d1p1=\"http://www.w3.org/2001/XMLSchema\" i:type=\"d1p1:int\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\">1</DCEmpty>");
  172. }
  173. void SerializeIntForDCEmpty (XmlObjectSerializer ser, string expected)
  174. {
  175. StringWriter sw = new StringWriter ();
  176. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  177. ser.WriteObject (w, 1);
  178. }
  179. XmlComparer.AssertAreEqual (expected, sw.ToString ());
  180. }
  181. // DCEmpty
  182. [Test]
  183. public void SerializeEmptyClass ()
  184. {
  185. DataContractSerializer ser =
  186. new DataContractSerializer (typeof (DCEmpty));
  187. SerializeEmptyClass (ser, "<DCEmpty xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" />");
  188. }
  189. [Test]
  190. [Category ("NotWorking")]
  191. public void NetSerializeEmptyClass ()
  192. {
  193. NetDataContractSerializer ser =
  194. new NetDataContractSerializer ();
  195. SerializeEmptyClass (ser, String.Format ("<DCEmpty xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" z:Id=\"1\" z:Type=\"MonoTests.System.Runtime.Serialization.DCEmpty\" z:Assembly=\"{0}\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" />", this.GetType ().Assembly.FullName));
  196. }
  197. void SerializeEmptyClass (XmlObjectSerializer ser, string expected)
  198. {
  199. StringWriter sw = new StringWriter ();
  200. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  201. ser.WriteObject (w, new DCEmpty ());
  202. }
  203. Assert.AreEqual (expected, sw.ToString ());
  204. }
  205. // DCEmpty
  206. [Test]
  207. public void SerializeEmptyNoNSClass ()
  208. {
  209. var ser = new DataContractSerializer (typeof (DCEmptyNoNS));
  210. SerializeEmptyNoNSClass (ser, "<DCEmptyNoNS xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" />");
  211. }
  212. void SerializeEmptyNoNSClass (XmlObjectSerializer ser, string expected)
  213. {
  214. var sw = new StringWriter ();
  215. using (var w = XmlWriter.Create (sw, settings)) {
  216. ser.WriteObject (w, new DCEmptyNoNS ());
  217. }
  218. Assert.AreEqual (expected, sw.ToString ());
  219. }
  220. // string (primitive)
  221. [Test]
  222. public void SerializePrimitiveString ()
  223. {
  224. XmlObjectSerializer ser =
  225. new DataContractSerializer (typeof (string));
  226. SerializePrimitiveString (ser, "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">TEST</string>");
  227. }
  228. [Test]
  229. [Category ("NotWorking")]
  230. public void NetSerializePrimitiveString ()
  231. {
  232. XmlObjectSerializer ser = new NetDataContractSerializer ();
  233. SerializePrimitiveString (ser, "<string z:Type=\"System.String\" z:Assembly=\"0\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\" xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">TEST</string>");
  234. }
  235. void SerializePrimitiveString (XmlObjectSerializer ser, string expected)
  236. {
  237. StringWriter sw = new StringWriter ();
  238. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  239. ser.WriteObject (w, "TEST");
  240. }
  241. Assert.AreEqual (expected, sw.ToString ());
  242. }
  243. // QName (primitive but ...)
  244. [Test]
  245. [Ignore ("These tests would not make any sense right now since it's populated prefix is not testable.")]
  246. public void SerializePrimitiveQName ()
  247. {
  248. XmlObjectSerializer ser =
  249. new DataContractSerializer (typeof (XmlQualifiedName));
  250. SerializePrimitiveQName (ser, "<z:QName xmlns:d7=\"urn:foo\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\">d7:foo</z:QName>");
  251. }
  252. [Test]
  253. [Ignore ("These tests would not make any sense right now since it's populated prefix is not testable.")]
  254. public void NetSerializePrimitiveQName ()
  255. {
  256. XmlObjectSerializer ser = new NetDataContractSerializer ();
  257. SerializePrimitiveQName (ser, "<z:QName z:Type=\"System.Xml.XmlQualifiedName\" z:Assembly=\"System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" xmlns:d7=\"urn:foo\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\">d7:foo</z:QName>");
  258. }
  259. void SerializePrimitiveQName (XmlObjectSerializer ser, string expected)
  260. {
  261. StringWriter sw = new StringWriter ();
  262. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  263. ser.WriteObject (w, new XmlQualifiedName ("foo", "urn:foo"));
  264. }
  265. Assert.AreEqual (expected, sw.ToString ());
  266. }
  267. // DCSimple1
  268. [Test]
  269. public void SerializeSimpleClass1 ()
  270. {
  271. DataContractSerializer ser =
  272. new DataContractSerializer (typeof (DCSimple1));
  273. SerializeSimpleClass1 (ser, "<DCSimple1 xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\"><Foo>TEST</Foo></DCSimple1>");
  274. }
  275. [Test]
  276. [ExpectedException (typeof (SerializationException))]
  277. public void SerializeSimpleXml ()
  278. {
  279. DataContractSerializer ser =
  280. new DataContractSerializer (typeof (SimpleXml));
  281. SerializeSimpleClass1 (ser, @"<simple i:type=""d1p1:DCSimple1"" xmlns:d1p1=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><d1p1:Foo>TEST</d1p1:Foo></simple>");
  282. }
  283. [Test]
  284. [Category ("NotWorking")]
  285. public void NetSerializeSimpleClass1 ()
  286. {
  287. NetDataContractSerializer ser =
  288. new NetDataContractSerializer ();
  289. SerializeSimpleClass1 (ser, String.Format ("<DCSimple1 xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" z:Id=\"1\" z:Type=\"MonoTests.System.Runtime.Serialization.DCSimple1\" z:Assembly=\"{0}\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\"><Foo z:Id=\"2\">TEST</Foo></DCSimple1>", this.GetType ().Assembly.FullName));
  290. }
  291. void SerializeSimpleClass1 (XmlObjectSerializer ser, string expected)
  292. {
  293. StringWriter sw = new StringWriter ();
  294. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  295. ser.WriteObject (w, new DCSimple1 ());
  296. }
  297. Console.WriteLine(sw.ToString());
  298. Assert.AreEqual (expected, sw.ToString ());
  299. }
  300. // NonDC (behavior changed in 3.5/SP1; not it's not rejected)
  301. [Test]
  302. public void SerializeNonDC ()
  303. {
  304. DataContractSerializer ser = new DataContractSerializer (typeof (NonDC));
  305. var sw = new StringWriter ();
  306. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  307. ser.WriteObject (w, new NonDC ());
  308. }
  309. Assert.AreEqual ("<NonDC xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Whee>whee!</Whee></NonDC>".Replace ('\'', '"'), sw.ToString ());
  310. }
  311. // DCHasNonDC
  312. [Test]
  313. public void SerializeDCHasNonDC ()
  314. {
  315. DataContractSerializer ser = new DataContractSerializer (typeof (DCHasNonDC));
  316. var sw = new StringWriter ();
  317. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  318. ser.WriteObject (w, new DCHasNonDC ());
  319. }
  320. Assert.AreEqual ("<DCHasNonDC xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Hoge><Whee>whee!</Whee></Hoge></DCHasNonDC>".Replace ('\'', '"'), sw.ToString ());
  321. }
  322. // DCHasSerializable
  323. [Test]
  324. // DCHasSerializable itself is DataContract and has a field
  325. // whose type is not contract but serializable.
  326. public void SerializeSimpleSerializable1 ()
  327. {
  328. DataContractSerializer ser = new DataContractSerializer (typeof (DCHasSerializable));
  329. StringWriter sw = new StringWriter ();
  330. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  331. ser.WriteObject (w, new DCHasSerializable ());
  332. }
  333. Assert.AreEqual ("<DCHasSerializable xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\"><Ser><Doh>doh!</Doh></Ser></DCHasSerializable>", sw.ToString ());
  334. }
  335. [Test]
  336. public void SerializeDCWithName ()
  337. {
  338. DataContractSerializer ser = new DataContractSerializer (typeof (DCWithName));
  339. StringWriter sw = new StringWriter ();
  340. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  341. ser.WriteObject (w, new DCWithName ());
  342. }
  343. Assert.AreEqual ("<Foo xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\"><FooMember>value</FooMember></Foo>", sw.ToString ());
  344. }
  345. [Test]
  346. public void SerializeDCWithEmptyName1 ()
  347. {
  348. DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEmptyName));
  349. StringWriter sw = new StringWriter ();
  350. DCWithEmptyName dc = new DCWithEmptyName ();
  351. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  352. try {
  353. ser.WriteObject (w, dc);
  354. } catch (InvalidDataContractException) {
  355. return;
  356. }
  357. }
  358. Assert.Fail ("Expected InvalidDataContractException");
  359. }
  360. [Test]
  361. [Category ("NotWorking")]
  362. public void SerializeDCWithEmptyName2 ()
  363. {
  364. DataContractSerializer ser = new DataContractSerializer (typeof (DCWithName));
  365. StringWriter sw = new StringWriter ();
  366. /* DataContractAttribute.Name == "", not valid */
  367. DCWithEmptyName dc = new DCWithEmptyName ();
  368. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  369. try {
  370. ser.WriteObject (w, dc);
  371. } catch (InvalidDataContractException) {
  372. return;
  373. }
  374. }
  375. Assert.Fail ("Expected InvalidDataContractException");
  376. }
  377. [Test]
  378. [Category ("NotWorking")]
  379. public void SerializeDCWithNullName ()
  380. {
  381. DataContractSerializer ser = new DataContractSerializer (typeof (DCWithNullName));
  382. StringWriter sw = new StringWriter ();
  383. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  384. try {
  385. /* DataContractAttribute.Name == "", not valid */
  386. ser.WriteObject (w, new DCWithNullName ());
  387. } catch (InvalidDataContractException) {
  388. return;
  389. }
  390. }
  391. Assert.Fail ("Expected InvalidDataContractException");
  392. }
  393. [Test]
  394. public void SerializeDCWithEmptyNamespace1 ()
  395. {
  396. DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEmptyNamespace));
  397. StringWriter sw = new StringWriter ();
  398. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  399. ser.WriteObject (w, new DCWithEmptyNamespace ());
  400. }
  401. }
  402. // Wrapper.DCWrapped
  403. [Test]
  404. public void SerializeWrappedClass ()
  405. {
  406. DataContractSerializer ser =
  407. new DataContractSerializer (typeof (Wrapper.DCWrapped));
  408. SerializeWrappedClass (ser, "<Wrapper.DCWrapped xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" />");
  409. }
  410. [Test]
  411. [Category ("NotWorking")]
  412. public void NetSerializeWrappedClass ()
  413. {
  414. NetDataContractSerializer ser =
  415. new NetDataContractSerializer ();
  416. SerializeWrappedClass (ser, String.Format ("<Wrapper.DCWrapped xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" z:Id=\"1\" z:Type=\"MonoTests.System.Runtime.Serialization.Wrapper+DCWrapped\" z:Assembly=\"{0}\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" />", this.GetType ().Assembly.FullName));
  417. }
  418. void SerializeWrappedClass (XmlObjectSerializer ser, string expected)
  419. {
  420. StringWriter sw = new StringWriter ();
  421. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  422. ser.WriteObject (w, new Wrapper.DCWrapped ());
  423. }
  424. Assert.AreEqual (expected, sw.ToString ());
  425. }
  426. [Test]
  427. /* old code
  428. // CollectionContainer : Items must have a setter.
  429. [ExpectedException (typeof (InvalidDataContractException))]
  430. [Category ("NotWorking")]
  431. */
  432. public void SerializeReadOnlyCollectionMember ()
  433. {
  434. DataContractSerializer ser =
  435. new DataContractSerializer (typeof (CollectionContainer));
  436. StringWriter sw = new StringWriter ();
  437. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  438. ser.WriteObject (w, null);
  439. }
  440. Assert.AreEqual ("<CollectionContainer i:nil='true' xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization' />".Replace ('\'', '"'), sw.ToString (), "#1");
  441. sw = new StringWriter ();
  442. var c = new CollectionContainer ();
  443. c.Items.Add ("foo");
  444. c.Items.Add ("bar");
  445. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  446. ser.WriteObject (w, c);
  447. }
  448. Assert.AreEqual ("<CollectionContainer xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Items xmlns:d2p1='http://schemas.microsoft.com/2003/10/Serialization/Arrays'><d2p1:string>foo</d2p1:string><d2p1:string>bar</d2p1:string></Items></CollectionContainer>".Replace ('\'', '"'), sw.ToString (), "#2");
  449. }
  450. // DataCollectionContainer : Items must have a setter.
  451. [Test]
  452. //[ExpectedException (typeof (InvalidDataContractException))]
  453. public void SerializeReadOnlyDataCollectionMember ()
  454. {
  455. DataContractSerializer ser =
  456. new DataContractSerializer (typeof (DataCollectionContainer));
  457. StringWriter sw = new StringWriter ();
  458. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  459. ser.WriteObject (w, null);
  460. }
  461. Assert.AreEqual ("<DataCollectionContainer i:nil='true' xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization' />".Replace ('\'', '"'), sw.ToString (), "#1");
  462. sw = new StringWriter ();
  463. var c = new DataCollectionContainer ();
  464. c.Items.Add ("foo");
  465. c.Items.Add ("bar");
  466. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  467. ser.WriteObject (w, c);
  468. }
  469. // LAMESPEC: this is bogus behavior. .NET serializes
  470. // System.String as "string" without overriding its
  471. // element namespace, but then it must be regarded as
  472. // in parent's namespace. What if there already is an
  473. // element definition for "string" with the same
  474. // namespace?
  475. Assert.AreEqual ("<DataCollectionContainer xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Items><string>foo</string><string>bar</string></Items></DataCollectionContainer>".Replace ('\'', '"'), sw.ToString (), "#2");
  476. }
  477. [Test]
  478. public void SerializeGuid ()
  479. {
  480. DataContractSerializer ser = new DataContractSerializer (typeof (Guid));
  481. StringWriter sw = new StringWriter ();
  482. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  483. ser.WriteObject (w, Guid.Empty);
  484. }
  485. Assert.AreEqual (
  486. "<guid xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">00000000-0000-0000-0000-000000000000</guid>",
  487. sw.ToString ());
  488. }
  489. [Test]
  490. public void SerializeEnum ()
  491. {
  492. DataContractSerializer ser = new DataContractSerializer (typeof (Colors));
  493. StringWriter sw = new StringWriter ();
  494. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  495. ser.WriteObject (w, new Colors ());
  496. }
  497. Assert.AreEqual (
  498. @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">Red</Colors>",
  499. sw.ToString ());
  500. }
  501. [Test]
  502. public void SerializeEnum2 ()
  503. {
  504. DataContractSerializer ser = new DataContractSerializer (typeof (Colors));
  505. StringWriter sw = new StringWriter ();
  506. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  507. ser.WriteObject (w, 0);
  508. }
  509. XmlComparer.AssertAreEqual (
  510. @"<Colors xmlns:d1p1=""http://www.w3.org/2001/XMLSchema"" i:type=""d1p1:int"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">0</Colors>",
  511. sw.ToString ());
  512. }
  513. [Test]
  514. public void SerializeEnumWithDC ()
  515. {
  516. DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
  517. StringWriter sw = new StringWriter ();
  518. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  519. ser.WriteObject (w, new ColorsWithDC ());
  520. }
  521. Assert.AreEqual (
  522. @"<_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">_Red</_ColorsWithDC>",
  523. sw.ToString ());
  524. }
  525. [Test]
  526. public void SerializeEnumWithNoDC ()
  527. {
  528. DataContractSerializer ser = new DataContractSerializer (typeof (ColorsEnumMemberNoDC));
  529. StringWriter sw = new StringWriter ();
  530. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  531. ser.WriteObject (w, new ColorsEnumMemberNoDC ());
  532. }
  533. Assert.AreEqual (
  534. @"<ColorsEnumMemberNoDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">Red</ColorsEnumMemberNoDC>",
  535. sw.ToString ());
  536. }
  537. [Test]
  538. public void SerializeEnumWithDC2 ()
  539. {
  540. DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
  541. StringWriter sw = new StringWriter ();
  542. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  543. ser.WriteObject (w, 3);
  544. }
  545. XmlComparer.AssertAreEqual (
  546. @"<_ColorsWithDC xmlns:d1p1=""http://www.w3.org/2001/XMLSchema"" i:type=""d1p1:int"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">3</_ColorsWithDC>",
  547. sw.ToString ());
  548. }
  549. [Test]
  550. [ExpectedException (typeof (SerializationException))]
  551. public void SerializeEnumWithDCInvalid ()
  552. {
  553. DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
  554. StringWriter sw = new StringWriter ();
  555. ColorsWithDC cdc = ColorsWithDC.Blue;
  556. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  557. ser.WriteObject (w, cdc);
  558. }
  559. }
  560. [Test]
  561. public void SerializeDCWithEnum ()
  562. {
  563. DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEnum));
  564. StringWriter sw = new StringWriter ();
  565. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  566. ser.WriteObject (w, new DCWithEnum ());
  567. }
  568. Assert.AreEqual (
  569. @"<DCWithEnum xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><_colors>Red</_colors></DCWithEnum>",
  570. sw.ToString ());
  571. }
  572. [Test]
  573. public void SerializeDCWithTwoEnums ()
  574. {
  575. DataContractSerializer ser = new DataContractSerializer (typeof (DCWithTwoEnums));
  576. StringWriter sw = new StringWriter ();
  577. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  578. DCWithTwoEnums e = new DCWithTwoEnums ();
  579. e.colors = Colors.Blue;
  580. e.colors2 = Colors.Green;
  581. ser.WriteObject (w, e);
  582. }
  583. Assert.AreEqual (
  584. @"<DCWithTwoEnums xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><colors>Blue</colors><colors2>Green</colors2></DCWithTwoEnums>",
  585. sw.ToString ());
  586. }
  587. [Test]
  588. public void SerializeNestingDC2 ()
  589. {
  590. DataContractSerializer ser = new DataContractSerializer (typeof (NestingDC2));
  591. StringWriter sw = new StringWriter ();
  592. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  593. NestingDC2 e = new NestingDC2 ();
  594. e.Field = new NestedDC2 ("Something");
  595. ser.WriteObject (w, e);
  596. }
  597. Assert.AreEqual (
  598. @"<NestingDC2 xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""test2""><Field xmlns:d2p1=""test1""><d2p1:Name>Something</d2p1:Name></Field></NestingDC2>",
  599. sw.ToString ());
  600. }
  601. [Test]
  602. public void SerializeNestingDC ()
  603. {
  604. DataContractSerializer ser = new DataContractSerializer (typeof (NestingDC));
  605. StringWriter sw = new StringWriter ();
  606. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  607. NestingDC e = new NestingDC ();
  608. e.Field1 = new NestedDC ("test1");
  609. e.Field2 = new NestedDC ("test2");
  610. ser.WriteObject (w, e);
  611. }
  612. Assert.AreEqual (
  613. @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><Field1><Name>test1</Name></Field1><Field2><Name>test2</Name></Field2></NestingDC>",
  614. sw.ToString ());
  615. sw = new StringWriter ();
  616. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  617. NestingDC e = new NestingDC ();
  618. ser.WriteObject (w, e);
  619. }
  620. Assert.AreEqual (
  621. @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><Field1 i:nil=""true"" /><Field2 i:nil=""true"" /></NestingDC>",
  622. sw.ToString ());
  623. }
  624. [Test]
  625. public void SerializeDerivedDC ()
  626. {
  627. DataContractSerializer ser = new DataContractSerializer (typeof (DerivedDC));
  628. StringWriter sw = new StringWriter ();
  629. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  630. DerivedDC e = new DerivedDC ();
  631. ser.WriteObject (w, e);
  632. }
  633. Assert.AreEqual (
  634. @"<DerivedDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""Derived""><baseVal xmlns=""Base"">0</baseVal><derivedVal>0</derivedVal></DerivedDC>",
  635. sw.ToString ());
  636. }
  637. [Test]
  638. public void SerializerDCArray ()
  639. {
  640. DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEnum []));
  641. StringWriter sw = new StringWriter ();
  642. DCWithEnum [] arr = new DCWithEnum [2];
  643. arr [0] = new DCWithEnum (); arr [0].colors = Colors.Red;
  644. arr [1] = new DCWithEnum (); arr [1].colors = Colors.Green;
  645. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  646. ser.WriteObject (w, arr);
  647. }
  648. XmlComparer.AssertAreEqual (
  649. @"<ArrayOfDCWithEnum xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><DCWithEnum><_colors>Red</_colors></DCWithEnum><DCWithEnum><_colors>Green</_colors></DCWithEnum></ArrayOfDCWithEnum>",
  650. sw.ToString ());
  651. }
  652. [Test]
  653. public void SerializerDCArray2 ()
  654. {
  655. List<Type> known = new List<Type> ();
  656. known.Add (typeof (DCWithEnum));
  657. known.Add (typeof (DCSimple1));
  658. DataContractSerializer ser = new DataContractSerializer (typeof (object []), known);
  659. StringWriter sw = new StringWriter ();
  660. object [] arr = new object [2];
  661. arr [0] = new DCWithEnum (); ((DCWithEnum)arr [0]).colors = Colors.Red;
  662. arr [1] = new DCSimple1 (); ((DCSimple1) arr [1]).Foo = "hello";
  663. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  664. ser.WriteObject (w, arr);
  665. }
  666. XmlComparer.AssertAreEqual (
  667. @"<ArrayOfanyType xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><anyType xmlns:d2p1=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"" i:type=""d2p1:DCWithEnum""><d2p1:_colors>Red</d2p1:_colors></anyType><anyType xmlns:d2p1=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"" i:type=""d2p1:DCSimple1""><d2p1:Foo>hello</d2p1:Foo></anyType></ArrayOfanyType>",
  668. sw.ToString ());
  669. }
  670. [Test]
  671. public void SerializerDCArray3 ()
  672. {
  673. DataContractSerializer ser = new DataContractSerializer (typeof (int []));
  674. StringWriter sw = new StringWriter ();
  675. int [] arr = new int [2];
  676. arr [0] = 1; arr [1] = 2;
  677. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  678. ser.WriteObject (w, arr);
  679. }
  680. XmlComparer.AssertAreEqual (
  681. @"<ArrayOfint xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><int>1</int><int>2</int></ArrayOfint>",
  682. sw.ToString ());
  683. }
  684. [Test]
  685. public void SerializeNonDCArray ()
  686. {
  687. DataContractSerializer ser = new DataContractSerializer (typeof (SerializeNonDCArrayType));
  688. StringWriter sw = new StringWriter ();
  689. using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
  690. ser.WriteObject (xw, new SerializeNonDCArrayType ());
  691. }
  692. Assert.AreEqual (@"<SerializeNonDCArrayType xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><IPAddresses /></SerializeNonDCArrayType>",
  693. sw.ToString ());
  694. }
  695. [Test]
  696. public void SerializeNonDCArrayItems ()
  697. {
  698. DataContractSerializer ser = new DataContractSerializer (typeof (SerializeNonDCArrayType));
  699. StringWriter sw = new StringWriter ();
  700. using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
  701. SerializeNonDCArrayType obj = new SerializeNonDCArrayType ();
  702. obj.IPAddresses = new NonDCItem [] {new NonDCItem () { Data = new int [] {1, 2, 3, 4} } };
  703. ser.WriteObject (xw, obj);
  704. }
  705. XmlDocument doc = new XmlDocument ();
  706. doc.LoadXml (sw.ToString ());
  707. XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
  708. nsmgr.AddNamespace ("s", "http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization");
  709. nsmgr.AddNamespace ("n", "http://schemas.datacontract.org/2004/07/System.Net");
  710. nsmgr.AddNamespace ("a", "http://schemas.microsoft.com/2003/10/Serialization/Arrays");
  711. Assert.AreEqual (1, doc.SelectNodes ("/s:SerializeNonDCArrayType/s:IPAddresses/s:NonDCItem", nsmgr).Count, "#1");
  712. XmlElement el = doc.SelectSingleNode ("/s:SerializeNonDCArrayType/s:IPAddresses/s:NonDCItem/s:Data", nsmgr) as XmlElement;
  713. Assert.IsNotNull (el, "#3");
  714. Assert.AreEqual (4, el.SelectNodes ("a:int", nsmgr).Count, "#4");
  715. }
  716. [Test]
  717. public void DeserializeEnum ()
  718. {
  719. Colors c = Deserialize<Colors> (
  720. @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">Red</Colors>");
  721. Assert.AreEqual (Colors.Red, c, "#de2");
  722. }
  723. [Test]
  724. public void DeserializeEnum2 ()
  725. {
  726. Colors c = Deserialize<Colors> (
  727. @"<Colors xmlns:d1p1=""http://www.w3.org/2001/XMLSchema"" i:type=""d1p1:int"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">1</Colors>",
  728. typeof (int));
  729. Assert.AreEqual (Colors.Green, c, "#de4");
  730. }
  731. [Test]
  732. [ExpectedException (typeof (SerializationException))]
  733. public void DeserializeEnumInvalid1 ()
  734. {
  735. Deserialize<Colors> (
  736. @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""></Colors>");
  737. }
  738. [Test]
  739. [ExpectedException (typeof (SerializationException))]
  740. public void DeserializeEnumInvalid2 ()
  741. {
  742. Deserialize<Colors> (
  743. @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""/>");
  744. }
  745. [Test]
  746. [ExpectedException (typeof (SerializationException))]
  747. public void DeserializeEnumInvalid3 ()
  748. {
  749. //"red" instead of "Red"
  750. Deserialize<Colors> (
  751. @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">red</Colors>");
  752. }
  753. [Test]
  754. public void DeserializeEnumFlags ()
  755. {
  756. Deserialize<Colors2> (
  757. @"<Colors2 xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""/>");
  758. }
  759. [Test]
  760. public void DeserializeEnumWithDC ()
  761. {
  762. ColorsWithDC cdc = Deserialize<ColorsWithDC> (
  763. @"<_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">_Red</_ColorsWithDC>");
  764. Assert.AreEqual (ColorsWithDC.Red, cdc, "#de6");
  765. }
  766. [Test]
  767. [ExpectedException (typeof (SerializationException))]
  768. public void DeserializeEnumWithDCInvalid ()
  769. {
  770. Deserialize<ColorsWithDC> (
  771. @"<_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">NonExistant</_ColorsWithDC>");
  772. }
  773. [Test]
  774. public void DeserializeDCWithEnum ()
  775. {
  776. DCWithEnum dc = Deserialize<DCWithEnum> (
  777. @"<DCWithEnum xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><_colors>Red</_colors></DCWithEnum>");
  778. Assert.AreEqual (Colors.Red, dc.colors, "#de8");
  779. }
  780. [Test]
  781. public void DeserializeNestingDC ()
  782. {
  783. NestingDC dc = Deserialize<NestingDC> (
  784. @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><Field1><Name>test1</Name></Field1><Field2><Name>test2</Name></Field2></NestingDC>");
  785. Assert.IsNotNull (dc.Field1, "#N1: Field1 should not be null.");
  786. Assert.IsNotNull (dc.Field2, "#N2: Field2 should not be null.");
  787. Assert.AreEqual ("test1", dc.Field1.Name, "#1");
  788. Assert.AreEqual ("test2", dc.Field2.Name, "#2");
  789. }
  790. [Test]
  791. public void DeserializeNestingDC2 ()
  792. {
  793. NestingDC2 dc = Deserialize<NestingDC2> (
  794. @"<NestingDC2 xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""test2""><Field xmlns:d2p1=""test1""><d2p1:Name>Something</d2p1:Name></Field></NestingDC2>");
  795. Assert.IsNotNull (dc.Field, "#N1: Field should not be null.");
  796. Assert.AreEqual ("Something", dc.Field.Name, "#N2");
  797. }
  798. [Test]
  799. public void DeserializeDerivedDC ()
  800. {
  801. DerivedDC dc = Deserialize<DerivedDC> (
  802. @"<DerivedDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""Derived""><baseVal xmlns=""Base"">1</baseVal><derivedVal>2</derivedVal></DerivedDC>");
  803. Assert.AreEqual (1, dc.baseVal, "#N1");
  804. Assert.AreEqual (2, dc.derivedVal, "#N2");
  805. }
  806. [Test]
  807. public void DeserializeTwice ()
  808. {
  809. string xml =
  810. @"<any><_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">_Red</_ColorsWithDC> <_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">_Red</_ColorsWithDC></any>";
  811. DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
  812. XmlReader xr = XmlReader.Create (new StringReader (xml), new XmlReaderSettings ());
  813. xr.ReadStartElement ();
  814. object o = ser.ReadObject (xr);
  815. Assert.AreEqual (typeof (ColorsWithDC), o.GetType (), "#de5");
  816. ColorsWithDC cdc = (ColorsWithDC) o;
  817. Assert.AreEqual (ColorsWithDC.Red, o, "#de6");
  818. o = ser.ReadObject (xr);
  819. Assert.AreEqual (typeof (ColorsWithDC), o.GetType (), "#de5");
  820. cdc = (ColorsWithDC) o;
  821. Assert.AreEqual (ColorsWithDC.Red, o, "#de6");
  822. Assert.AreEqual (XmlNodeType.EndElement, xr.NodeType, "#de6");
  823. Assert.AreEqual ("any", xr.LocalName, "#de6");
  824. xr.ReadEndElement ();
  825. }
  826. [Test]
  827. public void DeserializeEmptyNestingDC ()
  828. {
  829. NestingDC dc = Deserialize<NestingDC> (
  830. @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""></NestingDC>");
  831. Assert.IsNotNull (dc, "#A0: The object should not be null.");
  832. Assert.IsNull (dc.Field1, "#A1: Field1 should be null.");
  833. Assert.IsNull (dc.Field2, "#A2: Field2 should be null.");
  834. dc = Deserialize<NestingDC> (
  835. @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""/>");
  836. Assert.IsNotNull (dc, "#B0: The object should not be null.");
  837. Assert.IsNull (dc.Field1, "#B1: Field1 should be null.");
  838. Assert.IsNull (dc.Field2, "#B2: Field2 should be null.");
  839. dc = Deserialize<NestingDC> (
  840. @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><Field1 i:nil=""true"" /><Field2 i:nil=""true"" /></NestingDC>");
  841. Assert.IsNotNull (dc, "#B0: The object should not be null.");
  842. Assert.IsNull (dc.Field1, "#B1: Field1 should be null.");
  843. Assert.IsNull (dc.Field2, "#B2: Field2 should be null.");
  844. }
  845. [Test]
  846. [ExpectedException (typeof (SerializationException))]
  847. public void DeserializeEmptyDCWithTwoEnums ()
  848. {
  849. Deserialize<DCWithTwoEnums> (
  850. @"<DCWithTwoEnums xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><colors i:nil=""true""/><colors2 i:nil=""true""/></DCWithTwoEnums>");
  851. }
  852. [Test]
  853. public void DeserializeDCWithNullableEnum ()
  854. {
  855. DCWithNullableEnum dc = Deserialize<DCWithNullableEnum> (
  856. @"<DCWithNullableEnum xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><colors i:nil=""true""/></DCWithNullableEnum>");
  857. Assert.IsNull (dc.colors, "#B1: Field should be null.");
  858. }
  859. [Test]
  860. public void DeserializeDCWithTwoEnums ()
  861. {
  862. DCWithTwoEnums dc = Deserialize<DCWithTwoEnums> (
  863. @"<DCWithTwoEnums xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><colors>Blue</colors><colors2>Green</colors2></DCWithTwoEnums>");
  864. Assert.AreEqual (Colors.Blue, dc.colors, "#0");
  865. Assert.AreEqual (Colors.Green, dc.colors2, "#1");
  866. }
  867. [Test]
  868. public void DeserializerDCArray ()
  869. {
  870. DCWithEnum [] dcArray = Deserialize<DCWithEnum []> (
  871. @"<ArrayOfDCWithEnum xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><DCWithEnum><_colors>Red</_colors></DCWithEnum><DCWithEnum><_colors>Green</_colors></DCWithEnum></ArrayOfDCWithEnum>");
  872. Assert.AreEqual (2, dcArray.Length, "#N1");
  873. Assert.AreEqual (Colors.Red, dcArray [0].colors, "#N2");
  874. Assert.AreEqual (Colors.Green, dcArray [1].colors, "#N3");
  875. }
  876. [Test]
  877. public void DeserializerDCArray2 ()
  878. {
  879. string xml =
  880. @"<ArrayOfanyType xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><anyType xmlns:d2p1=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"" i:type=""d2p1:DCWithEnum""><d2p1:_colors>Red</d2p1:_colors></anyType><anyType xmlns:d2p1=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"" i:type=""d2p1:DCSimple1""><d2p1:Foo>hello</d2p1:Foo></anyType></ArrayOfanyType>";
  881. List<Type> known = new List<Type> ();
  882. known.Add (typeof (DCWithEnum));
  883. known.Add (typeof (DCSimple1));
  884. DataContractSerializer ser = new DataContractSerializer (typeof (object []), known);
  885. XmlReader xr = XmlReader.Create (new StringReader (xml));
  886. object [] dc = (object []) ser.ReadObject (xr);
  887. Assert.AreEqual (2, dc.Length, "#N1");
  888. Assert.AreEqual (typeof (DCWithEnum), dc [0].GetType (), "#N2");
  889. DCWithEnum dc0 = (DCWithEnum) dc [0];
  890. Assert.AreEqual (Colors.Red, dc0.colors, "#N3");
  891. Assert.AreEqual (typeof (DCSimple1), dc [1].GetType (), "#N4");
  892. DCSimple1 dc1 = (DCSimple1) dc [1];
  893. Assert.AreEqual ("hello", dc1.Foo, "#N4");
  894. }
  895. [Test]
  896. public void DeserializerDCArray3 ()
  897. {
  898. int [] intArray = Deserialize<int []> (
  899. @"<ArrayOfint xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><int>1</int><int>2</int></ArrayOfint>");
  900. Assert.AreEqual (2, intArray.Length, "#N0");
  901. Assert.AreEqual (1, intArray [0], "#N1");
  902. Assert.AreEqual (2, intArray [1], "#N2");
  903. }
  904. [Test]
  905. public void ReadObjectNoVerifyObjectName ()
  906. {
  907. string xml = @"<any><Member1 xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization1"">bar1</Member1><Member1 xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization2"">bar2</Member1><Member1 xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">bar</Member1></any>";
  908. VerifyObjectNameTestData res = (VerifyObjectNameTestData)new DataContractSerializer (typeof (VerifyObjectNameTestData))
  909. .ReadObject (XmlReader.Create (new StringReader (xml)), false);
  910. Assert.AreEqual ("bar", res.GetMember());
  911. }
  912. [Test]
  913. public void ReadObjectVerifyObjectName ()
  914. {
  915. string xml = @"<VerifyObjectNameTestData xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><Member1>bar</Member1></VerifyObjectNameTestData>";
  916. VerifyObjectNameTestData res = (VerifyObjectNameTestData)new DataContractSerializer (typeof (VerifyObjectNameTestData))
  917. .ReadObject (XmlReader.Create (new StringReader (xml)));
  918. Assert.AreEqual ("bar", res.GetMember());
  919. }
  920. [Test]
  921. [ExpectedException (typeof (SerializationException))]
  922. public void ReadObjectWrongNamespace ()
  923. {
  924. string xml = @"<VerifyObjectNameTestData xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization2""><Member1>bar</Member1></VerifyObjectNameTestData>";
  925. new DataContractSerializer (typeof (VerifyObjectNameTestData))
  926. .ReadObject (XmlReader.Create (new StringReader (xml)));
  927. }
  928. [Test]
  929. public void ReferenceSerialization ()
  930. {
  931. var dc = new DataContractSerializer (typeof (ReferenceWrapper));
  932. var t = new ReferenceType ();
  933. StringWriter sw = new StringWriter ();
  934. using (var xw = XmlWriter.Create (sw)) {
  935. xw.WriteStartElement ("z", "root", "http://schemas.microsoft.com/2003/10/Serialization/");
  936. dc.WriteObject (xw, new ReferenceWrapper () {T = t, T2 = t});
  937. xw.WriteEndElement ();
  938. }
  939. string xml = @"<?xml version='1.0' encoding='utf-16'?><z:root xmlns:z='http://schemas.microsoft.com/2003/10/Serialization/'><ReferenceWrapper xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><T z:Id='i1'><F>x</F></T><T2 z:Ref='i1' /></ReferenceWrapper></z:root>";
  940. Assert.AreEqual (xml.Replace ('\'', '"'), sw.ToString (), "#1");
  941. ReferenceWrapper w;
  942. using (XmlReader r = XmlReader.Create (new StringReader (xml)))
  943. {
  944. r.ReadStartElement ();
  945. w = (ReferenceWrapper) dc.ReadObject (r);
  946. r.ReadEndElement ();
  947. }
  948. Assert.AreEqual (w.T, w.T2, "#2");
  949. }
  950. [Test]
  951. public void GenericSerialization ()
  952. {
  953. var sw = new StringWriter ();
  954. var ser = new DataContractSerializer (typeof (Foo<string,int,int>));
  955. using (var xw = XmlWriter.Create (sw))
  956. ser.WriteObject (xw, new Foo<string,int,int> () {Field = "f"
  957. });
  958. var s = sw.ToString ();
  959. var ret = (Foo<string,int,int>) ser.ReadObject (XmlReader.Create (new StringReader (s)));
  960. Assert.AreEqual ("f", ret.Field);
  961. }
  962. [Test]
  963. public void GenericCollectionSerialization ()
  964. {
  965. var l = new MyList ();
  966. l.Add ("foo");
  967. l.Add ("bar");
  968. var ds = new DataContractSerializer (typeof (MyList));
  969. var sw = new StringWriter ();
  970. using (var xw = XmlWriter.Create (sw))
  971. ds.WriteObject (xw, l);
  972. l = (MyList) ds.ReadObject (XmlReader.Create (new StringReader (sw.ToString ())));
  973. Assert.AreEqual (2, l.Count);
  974. }
  975. [Test]
  976. public void GenericListOfKeyValuePairSerialization ()
  977. {
  978. string xml = @"<?xml version='1.0' encoding='utf-16'?><ArrayOfKeyValuePairOfstringstring xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/System.Collections.Generic'><KeyValuePairOfstringstring><key>foo</key><value>bar</value></KeyValuePairOfstringstring></ArrayOfKeyValuePairOfstringstring>".Replace ('\'', '"');
  979. var ds = new DataContractSerializer (typeof (List<KeyValuePair<string,string>>));
  980. var d = new List<KeyValuePair<string,string>> ();
  981. d.Add (new KeyValuePair<string,string> ("foo", "bar"));
  982. var sw = new StringWriter ();
  983. using (var xw = XmlWriter.Create (sw))
  984. ds.WriteObject (xw, d);
  985. Assert.AreEqual (xml, sw.ToString (), "#1");
  986. d = (List<KeyValuePair<string,string>>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
  987. Assert.AreEqual (1, d.Count, "#2");
  988. Assert.AreEqual ("bar", d [0].Value, "#3");
  989. }
  990. [Test]
  991. public void GenericListOfDictionaryEntrySerialization ()
  992. {
  993. string xml = @"<?xml version='1.0' encoding='utf-16'?><ArrayOfDictionaryEntry xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/System.Collections'><DictionaryEntry><_key xmlns:d3p1='http://www.w3.org/2001/XMLSchema' i:type='d3p1:string'>foo</_key><_value xmlns:d3p1='http://www.w3.org/2001/XMLSchema' i:type='d3p1:string'>bar</_value></DictionaryEntry></ArrayOfDictionaryEntry>".Replace ('\'', '"');
  994. var ds = new DataContractSerializer (typeof (List<DictionaryEntry>));
  995. var d = new List<DictionaryEntry> ();
  996. d.Add (new DictionaryEntry ("foo", "bar"));
  997. var sw = new StringWriter ();
  998. using (var xw = XmlWriter.Create (sw))
  999. ds.WriteObject (xw, d);
  1000. Assert.AreEqual (xml, sw.ToString (), "#1");
  1001. Assert.IsTrue (sw.ToString ().IndexOf ("i:type") >= 0);
  1002. d = (List<DictionaryEntry>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
  1003. Assert.AreEqual (1, d.Count, "#2");
  1004. Assert.AreEqual ("bar", d [0].Value, "#3");
  1005. }
  1006. [Test]
  1007. public void GenericDictionarySerialization ()
  1008. {
  1009. string xml = @"<?xml version='1.0' encoding='utf-16'?><ArrayOfKeyValueOfstringstring xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.microsoft.com/2003/10/Serialization/Arrays'><KeyValueOfstringstring><Key>foo</Key><Value>bar</Value></KeyValueOfstringstring></ArrayOfKeyValueOfstringstring>".Replace ('\'', '"');
  1010. var ds = new DataContractSerializer (typeof (Dictionary<string,string>));
  1011. var d = new Dictionary<string,string> ();
  1012. d ["foo"] = "bar";
  1013. var sw = new StringWriter ();
  1014. using (var xw = XmlWriter.Create (sw))
  1015. ds.WriteObject (xw, d);
  1016. Assert.AreEqual (xml, sw.ToString (), "#1");
  1017. d = (Dictionary<string,string>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
  1018. Assert.AreEqual (1, d.Count, "#2");
  1019. Assert.AreEqual ("bar", d ["foo"], "#3");
  1020. }
  1021. [Test]
  1022. public void HashtableSerialization ()
  1023. {
  1024. string xml = @"<?xml version='1.0' encoding='utf-16'?><ArrayOfKeyValueOfanyTypeanyType xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.microsoft.com/2003/10/Serialization/Arrays'><KeyValueOfanyTypeanyType><Key xmlns:d3p1='http://www.w3.org/2001/XMLSchema' i:type='d3p1:string'>foo</Key><Value xmlns:d3p1='http://www.w3.org/2001/XMLSchema' i:type='d3p1:string'>bar</Value></KeyValueOfanyTypeanyType></ArrayOfKeyValueOfanyTypeanyType>".Replace ('\'', '"');
  1025. var ds = new DataContractSerializer (typeof (Hashtable));
  1026. var d = new Hashtable ();
  1027. d ["foo"] = "bar";
  1028. var sw = new StringWriter ();
  1029. using (var xw = XmlWriter.Create (sw))
  1030. ds.WriteObject (xw, d);
  1031. Assert.AreEqual (xml, sw.ToString (), "#1");
  1032. d = (Hashtable) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
  1033. Assert.AreEqual (1, d.Count, "#2");
  1034. Assert.AreEqual ("bar", d ["foo"], "#3");
  1035. }
  1036. [Test]
  1037. public void CollectionContarctDictionarySerialization ()
  1038. {
  1039. string xml = @"<?xml version='1.0' encoding='utf-16'?><NAME xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='urn:foo'><ITEM><KEY>foo</KEY><VALUE>bar</VALUE></ITEM></NAME>".Replace ('\'', '"');
  1040. var ds = new DataContractSerializer (typeof (MyDictionary<string,string>));
  1041. var d = new MyDictionary<string,string> ();
  1042. d ["foo"] = "bar";
  1043. var sw = new StringWriter ();
  1044. using (var xw = XmlWriter.Create (sw))
  1045. ds.WriteObject (xw, d);
  1046. Assert.AreEqual (xml, sw.ToString (), "#1");
  1047. d = (MyDictionary<string,string>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
  1048. Assert.AreEqual (1, d.Count, "#2");
  1049. Assert.AreEqual ("bar", d ["foo"], "#3");
  1050. }
  1051. [Test]
  1052. public void SerializeInterfaceCollection ()
  1053. {
  1054. var ser = new DataContractSerializer (typeof (InterfaceCollectionType));
  1055. var sw = new StringWriter ();
  1056. var obj = new InterfaceCollectionType ();
  1057. using (var xw = XmlWriter.Create (sw))
  1058. ser.WriteObject (xw, obj);
  1059. using (var xr = XmlReader.Create (new StringReader (sw.ToString ()))) {
  1060. obj = (InterfaceCollectionType) ser.ReadObject (xr);
  1061. Assert.IsNull (obj.Array, "#1");
  1062. }
  1063. sw = new StringWriter ();
  1064. obj.Array = new List<int> ();
  1065. obj.Array.Add (5);
  1066. using (var xw = XmlWriter.Create (sw))
  1067. ser.WriteObject (xw, obj);
  1068. using (var xr = XmlReader.Create (new StringReader (sw.ToString ()))) {
  1069. obj = (InterfaceCollectionType) ser.ReadObject (xr);
  1070. Assert.AreEqual (5, obj.Array [0], "#2");
  1071. }
  1072. }
  1073. [Test]
  1074. public void EmptyChildren ()
  1075. {
  1076. string xml = @"
  1077. <DummyPlaylist xmlns='http://example.com/schemas/asx'>
  1078. <Entries>
  1079. <DummyEntry>
  1080. <EntryInfo xmlns:i='http://www.w3.org/2001/XMLSchema-instance' i:type='PartDummyEntryInfo'/>
  1081. <Href>http://vmsservices.example.com:8080/VideoService.svc?crid=45541/part=1/guid=ae968b5d-e4a5-41fe-9b23-ed631b27cd21/</Href>
  1082. </DummyEntry>
  1083. </Entries>
  1084. </DummyPlaylist>
  1085. ";
  1086. var reader = XmlReader.Create (new StringReader (xml));
  1087. DummyPlaylist playlist = (DummyPlaylist) new DataContractSerializer (typeof (DummyPlaylist)).ReadObject (reader);
  1088. Assert.AreEqual (1, playlist.entries.Count, "#1");
  1089. Assert.IsTrue (playlist.entries [0] is DummyEntry, "#2");
  1090. Assert.IsNotNull (playlist.entries [0].Href, "#3");
  1091. }
  1092. [Test]
  1093. public void BaseKnownTypeAttributes ()
  1094. {
  1095. // bug #524088
  1096. string xml = @"
  1097. <DummyPlaylist xmlns='http://example.com/schemas/asx'>
  1098. <Entries>
  1099. <DummyEntry>
  1100. <EntryInfo xmlns:i='http://www.w3.org/2001/XMLSchema-instance' i:type='PartDummyEntryInfo'/>
  1101. </DummyEntry>
  1102. </Entries>
  1103. </DummyPlaylist>";
  1104. using (XmlReader reader = XmlReader.Create (new StringReader (xml))) {
  1105. DummyPlaylist playlist = new DataContractSerializer(typeof(DummyPlaylist)).ReadObject(reader) as DummyPlaylist;
  1106. Assert.IsNotNull (playlist);
  1107. }
  1108. }
  1109. [Test]
  1110. public void Bug524083 ()
  1111. {
  1112. string xml = @"
  1113. <AsxEntryInfo xmlns='http://example.com/schemas/asx'>
  1114. <AdvertPrompt/>
  1115. </AsxEntryInfo>";
  1116. using (XmlReader reader = XmlReader.Create (new StringReader (xml)))
  1117. new DataContractSerializer(typeof (AsxEntryInfo)).ReadObject (reader);
  1118. }
  1119. [Test]
  1120. public void Bug539563 ()
  1121. {
  1122. new DataContractSerializer (typeof (NestedContractType));
  1123. }
  1124. [Test]
  1125. public void Bug560155 ()
  1126. {
  1127. var g = Guid.NewGuid ();
  1128. Person p1 = new Person ("UserName", g);
  1129. Assert.AreEqual ("name=UserName,id=" + g, p1.ToString (), "#1");
  1130. MemoryStream memStream = new MemoryStream ();
  1131. DataContractSerializer ser = new DataContractSerializer (typeof (Person));
  1132. ser.WriteObject (memStream, p1);
  1133. memStream.Seek (0, SeekOrigin.Begin);
  1134. Person p2 = (Person) ser.ReadObject (memStream);
  1135. Assert.AreEqual ("name=UserName,id=" + g, p2.ToString (), "#1");
  1136. }
  1137. private T Deserialize<T> (string xml)
  1138. {
  1139. return Deserialize<T> (xml, typeof (T));
  1140. }
  1141. private T Deserialize<T> (string xml, Type runtimeType)
  1142. {
  1143. DataContractSerializer ser = new DataContractSerializer (typeof (T));
  1144. XmlReader xr = XmlReader.Create (new StringReader (xml), new XmlReaderSettings ());
  1145. object o = ser.ReadObject (xr);
  1146. Assert.AreEqual (runtimeType, o.GetType (), "#DS0");
  1147. return (T)o;
  1148. }
  1149. public Dictionary<string, object> GenericDictionary (Dictionary<string, object> settings)
  1150. {
  1151. using (MemoryStream ms = new MemoryStream ()) {
  1152. DataContractSerializer save = new DataContractSerializer (settings.GetType ());
  1153. save.WriteObject (ms, settings);
  1154. ms.Position = 0;
  1155. DataContractSerializer load = new DataContractSerializer (typeof (Dictionary<string, object>));
  1156. return (Dictionary<string, object>) load.ReadObject (ms);
  1157. }
  1158. }
  1159. [Test]
  1160. public void GenericDictionaryEmpty ()
  1161. {
  1162. Dictionary<string, object> in_settings = new Dictionary<string, object> ();
  1163. Dictionary<string, object> out_settings = GenericDictionary (in_settings);
  1164. out_settings.Clear ();
  1165. }
  1166. [Test]
  1167. public void GenericDictionaryOneElement ()
  1168. {
  1169. Dictionary<string, object> in_settings = new Dictionary<string, object> ();
  1170. in_settings.Add ("one", "ONE");
  1171. Dictionary<string, object> out_settings = GenericDictionary (in_settings);
  1172. Assert.AreEqual ("ONE", out_settings ["one"], "out");
  1173. out_settings.Clear ();
  1174. }
  1175. [Test]
  1176. public void IgnoreDataMember ()
  1177. {
  1178. var ser = new DataContractSerializer (typeof (MemberIgnored));
  1179. var sw = new StringWriter ();
  1180. using (var w = XmlWriter.Create (sw, settings)) {
  1181. ser.WriteObject (w, new MemberIgnored ());
  1182. }
  1183. Assert.AreEqual (@"<MemberIgnored xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><body><Bar>bar</Bar></body></MemberIgnored>", sw.ToString (), "#1");
  1184. }
  1185. [Test]
  1186. public void DeserializeEmptyArray ()
  1187. {
  1188. var ds = new DataContractSerializer (typeof (string []));
  1189. var sw = new StringWriter ();
  1190. var xw = XmlWriter.Create (sw);
  1191. ds.WriteObject (xw, new string [] {});
  1192. xw.Close ();
  1193. Console.WriteLine (sw.ToString ());
  1194. var sr = new StringReader (sw.ToString ());
  1195. var xr = XmlReader.Create (sr);
  1196. var ret = ds.ReadObject (xr);
  1197. Assert.AreEqual (typeof (string []), ret.GetType (), "#1");
  1198. }
  1199. [Test]
  1200. public void ContractNamespaceAttribute ()
  1201. {
  1202. var ds = new DataContractSerializer (typeof (U2U.DataContracts.Person));
  1203. string xml = "<?xml version='1.0' encoding='utf-16'?><Person xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://www.u2u.be/samples/wcf/2009'><Name>Rupert</Name><Occupation><Description>Monkey</Description></Occupation></Person>";
  1204. var person = new U2U.DataContracts.Person () {
  1205. Name = "Rupert",
  1206. Occupation = new U2U.DataContracts.Job () { Description = "Monkey" }
  1207. };
  1208. var sw = new StringWriter ();
  1209. using (var xw = XmlWriter.Create (sw))
  1210. ds.WriteObject (xw, person);
  1211. Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
  1212. }
  1213. [Test]
  1214. public void Bug610036 ()
  1215. {
  1216. var ms = new MemoryStream ();
  1217. Type [] knownTypes = new Type [] { typeof (ParentClass), typeof (Foo), typeof (Bar) };
  1218. var ds = new DataContractSerializer (typeof (Root), "Root", "Company.Foo", knownTypes, 1000, false, true, null);
  1219. var root = new Root ("root");
  1220. var bar1 = new Bar ("bar1");
  1221. var bar2 = new Bar ("bar2");
  1222. var bar3 = new Bar ("bar3");
  1223. var foo1 = new Foo ("foo1");
  1224. var foo2 = new Foo ("foo2");
  1225. foo1.FDict.Add (bar1);
  1226. foo1.FDict.Add (bar2);
  1227. foo2.FDict.Add (bar1);
  1228. foo2.FDict.Add (bar3);
  1229. root.FDict.Add (foo1);
  1230. root.FDict.Add (foo2);
  1231. ds.WriteObject (ms, root);
  1232. string result = Encoding.UTF8.GetString (ms.ToArray ());
  1233. ms.Position = 0;
  1234. root = (Root) ds.ReadObject (ms);
  1235. Assert.AreEqual (2, root.FDict.Count, "#1");
  1236. int idx = result.IndexOf ("foo1");
  1237. Assert.IsTrue (idx >= 0, "#2");
  1238. // since "foo1" is stored as z:Ref for string, it must not occur twice.
  1239. int idx2 = result.IndexOf ("foo1", idx + 1);
  1240. Assert.IsTrue (idx2 < 0, "idx2 should not occur at " + idx2);
  1241. }
  1242. [Test]
  1243. public void AncestralReference ()
  1244. {
  1245. // Reference to Parent comes inside the Parent itself.
  1246. // In this case, adding reference after complete deserialization won't work (but it should).
  1247. var ms = new MemoryStream ();
  1248. Type [] knownTypes = new Type [] { typeof (ParentClass), typeof (Foo), typeof (Bar) };
  1249. var ds = new DataContractSerializer (typeof (Parent));
  1250. var org = new Parent ();
  1251. ds.WriteObject (ms, org);
  1252. string result = Encoding.UTF8.GetString (ms.ToArray ());
  1253. ms.Position = 0;
  1254. var parent = (Parent) ds.ReadObject (ms);
  1255. Assert.IsNotNull (parent.Child, "#1");
  1256. Assert.AreEqual (parent, parent.Child.Parent, "#2");
  1257. }
  1258. [Test]
  1259. public void IXmlSerializableCallConstructor ()
  1260. {
  1261. IXmlSerializableCallConstructor (false);
  1262. IXmlSerializableCallConstructor (true);
  1263. }
  1264. void IXmlSerializableCallConstructor (bool binary)
  1265. {
  1266. Stream s = IXmlSerializableCallConstructorSerialize (binary);
  1267. var a = new byte [s.Length];
  1268. s.Position = 0;
  1269. s.Read (a, 0, a.Length);
  1270. s.Position = 0;
  1271. IXmlSerializableCallConstructorDeserialize (s, binary);
  1272. }
  1273. public Stream IXmlSerializableCallConstructorSerialize (bool binary)
  1274. {
  1275. var ds = new DataSet ("ds");
  1276. var dt = new DataTable ("dt");
  1277. ds.Tables.Add (dt);
  1278. dt.Columns.Add ("n", typeof (int));
  1279. dt.Columns.Add ("s", typeof (string));
  1280. dt.Rows.Add (5, "five");
  1281. dt.Rows.Add (10, "ten");
  1282. ds.AcceptChanges ();
  1283. var s = new MemoryStream ();
  1284. var w = binary ? XmlDictionaryWriter.CreateBinaryWriter (s) : XmlDictionaryWriter.CreateTextWriter (s);
  1285. var x = new DataContractSerializer (typeof (DataSet));
  1286. x.WriteObject (w, ds);
  1287. w.Flush ();
  1288. return s;
  1289. }
  1290. public void IXmlSerializableCallConstructorDeserialize (Stream s, bool binary)
  1291. {
  1292. var r = binary ? XmlDictionaryReader.CreateBinaryReader (s, XmlDictionaryReaderQuotas.Max)
  1293. : XmlDictionaryReader.CreateTextReader (s, XmlDictionaryReaderQuotas.Max);
  1294. var x = new DataContractSerializer (typeof (DataSet));
  1295. var ds = (DataSet) x.ReadObject (r);
  1296. }
  1297. [Test]
  1298. [ExpectedException (typeof (InvalidDataContractException))] // BaseConstraintType1 is neither DataContract nor Serializable.
  1299. public void BaseConstraint1 ()
  1300. {
  1301. new DataContractSerializer (typeof (BaseConstraintType3)).WriteObject (XmlWriter.Create (TextWriter.Null), new BaseConstraintType3 ());
  1302. }
  1303. [Test]
  1304. public void BaseConstraint2 ()
  1305. {
  1306. new DataContractSerializer (typeof (BaseConstraintType4)).WriteObject (XmlWriter.Create (TextWriter.Null), new BaseConstraintType4 ());
  1307. }
  1308. [Test] // bug #652331
  1309. public void MembersNamespacesInBaseType ()
  1310. {
  1311. string xml1 = @"<Currency>JPY</Currency><Description i:nil=""true"" />";
  1312. string xml2 = @"<Currency xmlns=""http://schemas.datacontract.org/2004/07/SLProto5"">JPY</Currency><Description i:nil=""true"" xmlns=""http://schemas.datacontract.org/2004/07/SLProto5"" />";
  1313. Assert.AreEqual ("JPY", MembersNamespacesInBaseType_Part<SLProto5.CashAmount> (new SLProto5.CashAmount () { Currency = "JPY" }, xml1, "#1").Currency, "r#1");
  1314. Assert.AreEqual ("JPY", MembersNamespacesInBaseType_Part<SLProto5_Different.CashAmount> (new SLProto5_Different.CashAmount () { Currency = "JPY" }, xml2, "#2").Currency, "r#2");
  1315. Assert.AreEqual ("JPY", MembersNamespacesInBaseType_Part<SLProto5.CashAmountDC> (new SLProto5.CashAmountDC () { Currency = "JPY" }, xml1, "#3").Currency, "r#3");
  1316. Assert.AreEqual ("JPY", MembersNamespacesInBaseType_Part<SLProto5_Different.CashAmountDC> (new SLProto5_Different.CashAmountDC () { Currency = "JPY" }, xml2, "#4").Currency, "r#4");
  1317. }
  1318. T MembersNamespacesInBaseType_Part<T> (T instance, string expectedPart, string assert)
  1319. {
  1320. var ds = new DataContractSerializer (typeof (T));
  1321. var sw = new StringWriter ();
  1322. using (var w = XmlWriter.Create (sw))
  1323. ds.WriteObject (w, instance);
  1324. Assert.IsTrue (sw.ToString ().IndexOf (expectedPart) > 0, assert);
  1325. return (T) ds.ReadObject (XmlReader.Create (new StringReader (sw.ToString ())));
  1326. }
  1327. [Test]
  1328. public void DateTimeOffsetSerialization ()
  1329. {
  1330. var ds = new DataContractSerializer (typeof (DateTimeOffset));
  1331. var sw = new StringWriter ();
  1332. string xml = "<DateTimeOffset xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/System'><DateTime>2011-03-01T02:05:06.078Z</DateTime><OffsetMinutes>120</OffsetMinutes></DateTimeOffset>".Replace ('\'', '"');
  1333. var v = new DateTimeOffset (new DateTime (2011, 3, 1, 4, 5, 6, 78), TimeSpan.FromMinutes (120));
  1334. using (var xw = XmlWriter.Create (sw, settings)) {
  1335. ds.WriteObject (xw, v);
  1336. }
  1337. Assert.AreEqual (xml, sw.ToString (), "#1");
  1338. Assert.AreEqual (v, ds.ReadObject (XmlReader.Create (new StringReader (sw.ToString ()))), "#2");
  1339. }
  1340. [Test]
  1341. public void DateTimeOffsetNullableSerialization ()
  1342. {
  1343. var ds = new DataContractSerializer (typeof (DateTimeOffset?));
  1344. var sw = new StringWriter ();
  1345. string xml = "<DateTimeOffset xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/System\"><DateTime>2012-05-04T00:34:00Z</DateTime><OffsetMinutes>120</OffsetMinutes></DateTimeOffset>";
  1346. var v = new DateTimeOffset (new DateTime (2012, 05, 04, 02, 34, 0), TimeSpan.FromMinutes (120));
  1347. using (var xw = XmlWriter.Create (sw, settings)) {
  1348. ds.WriteObject (xw, v);
  1349. }
  1350. Assert.AreEqual (xml, sw.ToString (), "#1");
  1351. Assert.AreEqual (v, ds.ReadObject (XmlReader.Create (new StringReader (sw.ToString ()))), "#2");
  1352. }
  1353. [Test]
  1354. public void XmlDocumentSupport ()
  1355. {
  1356. var xml = "<XmlDocumentContract xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='urn:foo'><Content><Root xmlns=''>Hello, world!</Root></Content><Nodes><child1 xmlns='' /><child2 xmlns='' /></Nodes></XmlDocumentContract>".Replace ('\'', '"');
  1357. var xml2 = "<Root>Hello, world!</Root>";
  1358. var obj = new XmlDocumentContract ();
  1359. var doc = new XmlDocument ();
  1360. doc.LoadXml (xml2);
  1361. obj.Content = doc.DocumentElement;
  1362. doc = new XmlDocument ();
  1363. doc.LoadXml ("<root><child1/><child2/></root>");
  1364. var l = new List<XmlNode> ();
  1365. foreach (XmlNode node in doc.DocumentElement.ChildNodes)
  1366. l.Add (node);
  1367. obj.Nodes = l.ToArray ();
  1368. var serializer = new DataContractSerializer (typeof (XmlDocumentContract))
  1369. ;
  1370. var sb = new StringBuilder ();
  1371. using (var writer = new StringWriter (sb))
  1372. serializer.WriteObject (new XmlTextWriter (writer), obj);
  1373. Assert.AreEqual (xml, sb.ToString (), "#1");
  1374. using (var reader = new StringReader (sb.ToString ()))
  1375. obj = serializer.ReadObject (new XmlTextReader (reader)) as XmlDocumentContract;
  1376. Assert.AreEqual ("Hello, world!", obj.Content != null ? obj.Content.InnerText : String.Empty, "#2");
  1377. Assert.AreEqual (2, obj.Nodes != null ? obj.Nodes.Length : -1, "#3");
  1378. }
  1379. [Test]
  1380. public void ArrayAsEnumerableAsRoot ()
  1381. {
  1382. var ds = new DataContractSerializer (typeof (IEnumerable<Guid>));
  1383. var sw = new StringWriter ();
  1384. using (var xw = XmlWriter.Create (sw, settings))
  1385. ds.WriteObject (xw, new Guid [] {Guid.Empty});
  1386. string xml = "<ArrayOfguid xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.microsoft.com/2003/10/Serialization/Arrays'><guid>00000000-0000-0000-0000-000000000000</guid></ArrayOfguid>".Replace ('\'', '"');
  1387. Assert.AreEqual (xml, sw.ToString (), "#1");
  1388. }
  1389. // bug #7957
  1390. [Test]
  1391. public void DeserializeEmptyDictionary ()
  1392. {
  1393. string whatItGets = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
  1394. + "<MyData xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://sickhead.com/types/Example\">"
  1395. + "<Data xmlns:b=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\"/>"
  1396. + "<FirstId>b8a7eb6f-f593-4668-8178-07be9f7266d1</FirstId>"
  1397. + "<SecondId>ID-GOES-HERE</SecondId>"
  1398. + "</MyData>";
  1399. var serializer = new DataContractSerializer (typeof (MyData));
  1400. using (var stream = new MemoryStream (Encoding.UTF8.GetBytes (whatItGets)))
  1401. {
  1402. var data = serializer.ReadObject (stream);
  1403. }
  1404. }
  1405. }
  1406. [DataContract]
  1407. public class MemberIgnored
  1408. {
  1409. [DataMember]
  1410. MemberIgnoredBody body = new MemberIgnoredBody ();
  1411. }
  1412. public class MemberIgnoredBody
  1413. {
  1414. [IgnoreDataMember]
  1415. public string Foo = "foo";
  1416. public string Bar = "bar";
  1417. }
  1418. public enum Colors {
  1419. Red, Green, Blue
  1420. }
  1421. [Flags]
  1422. public enum Colors2 {
  1423. Red, Green, Blue
  1424. }
  1425. [DataContract (Name = "_ColorsWithDC")]
  1426. public enum ColorsWithDC {
  1427. [EnumMember (Value = "_Red")]
  1428. Red,
  1429. [EnumMember]
  1430. Green,
  1431. Blue
  1432. }
  1433. public enum ColorsEnumMemberNoDC {
  1434. [EnumMember (Value = "_Red")]
  1435. Red,
  1436. [EnumMember]
  1437. Green,
  1438. Blue
  1439. }
  1440. [DataContract]
  1441. public class DCWithEnum {
  1442. [DataMember (Name = "_colors")]
  1443. public Colors colors;
  1444. }
  1445. [DataContract]
  1446. public class DCWithTwoEnums {
  1447. [DataMember]
  1448. public Colors colors;
  1449. [DataMember]
  1450. public Colors colors2;
  1451. }
  1452. [DataContract]
  1453. public class DCWithNullableEnum {
  1454. [DataMember]
  1455. public Colors? colors;
  1456. }
  1457. [DataContract (Namespace = "Base")]
  1458. public class BaseDC {
  1459. [DataMember]
  1460. public int baseVal;
  1461. }
  1462. [DataContract (Namespace = "Derived")]
  1463. public class DerivedDC : BaseDC {
  1464. [DataMember]
  1465. public int derivedVal;
  1466. }
  1467. [DataContract]
  1468. public class NestedDC {
  1469. public NestedDC (string name) { this.Name = name; }
  1470. [DataMember]
  1471. public string Name;
  1472. }
  1473. [DataContract]
  1474. public class NestingDC {
  1475. [DataMember]
  1476. public NestedDC Field1;
  1477. [DataMember]
  1478. public NestedDC Field2;
  1479. }
  1480. [DataContract (Namespace = "test1")]
  1481. public class NestedDC2 {
  1482. public NestedDC2 (string name) { this.Name = name; }
  1483. [DataMember]
  1484. public string Name;
  1485. }
  1486. [DataContract (Namespace = "test2")]
  1487. public class NestingDC2 {
  1488. [DataMember]
  1489. public NestedDC2 Field;
  1490. }
  1491. [DataContract]
  1492. public class DCEmpty
  1493. {
  1494. // serializer doesn't touch it.
  1495. public string Foo = "TEST";
  1496. }
  1497. [DataContract (Namespace = "")]
  1498. public class DCEmptyNoNS
  1499. {
  1500. }
  1501. [DataContract]
  1502. public class DCSimple1
  1503. {
  1504. [DataMember]
  1505. public string Foo = "TEST";
  1506. }
  1507. [DataContract]
  1508. public class DCHasNonDC
  1509. {
  1510. [DataMember]
  1511. public NonDC Hoge= new NonDC ();
  1512. }
  1513. public class NonDC
  1514. {
  1515. public string Whee = "whee!";
  1516. }
  1517. [DataContract]
  1518. public class DCHasSerializable
  1519. {
  1520. [DataMember]
  1521. public SimpleSer1 Ser = new SimpleSer1 ();
  1522. }
  1523. [DataContract (Name = "Foo")]
  1524. public class DCWithName
  1525. {
  1526. [DataMember (Name = "FooMember")]
  1527. public string DMWithName = "value";
  1528. }
  1529. [DataContract (Name = "")]
  1530. public class DCWithEmptyName
  1531. {
  1532. }
  1533. [DataContract (Name = null)]
  1534. public class DCWithNullName
  1535. {
  1536. }
  1537. [DataContract (Namespace = "")]
  1538. public class DCWithEmptyNamespace
  1539. {
  1540. }
  1541. [Serializable]
  1542. public class SimpleSer1
  1543. {
  1544. public string Doh = "doh!";
  1545. [NonSerialized]
  1546. public string Bah = "bah!";
  1547. }
  1548. public class Wrapper
  1549. {
  1550. [DataContract]
  1551. public class DCWrapped
  1552. {
  1553. }
  1554. }
  1555. [DataContract]
  1556. public class CollectionContainer
  1557. {
  1558. Collection<string> items = new Collection<string> ();
  1559. [DataMember]
  1560. public Collection<string> Items {
  1561. get { return items; }
  1562. }
  1563. }
  1564. [CollectionDataContract]
  1565. public class DataCollection<T> : Collection<T>
  1566. {
  1567. }
  1568. [DataContract]
  1569. public class DataCollectionContainer
  1570. {
  1571. DataCollection<string> items = new DataCollection<string> ();
  1572. [DataMember]
  1573. public DataCollection<string> Items {
  1574. get { return items; }
  1575. }
  1576. }
  1577. [DataContract]
  1578. class SerializeNonDCArrayType
  1579. {
  1580. [DataMember]
  1581. public NonDCItem [] IPAddresses = new NonDCItem [0];
  1582. }
  1583. public class NonDCItem
  1584. {
  1585. public int [] Data { get; set; }
  1586. }
  1587. [DataContract]
  1588. public class VerifyObjectNameTestData
  1589. {
  1590. [DataMember]
  1591. string Member1 = "foo";
  1592. public string GetMember() { return Member1; }
  1593. }
  1594. [XmlRoot(ElementName = "simple", Namespace = "")]
  1595. public class SimpleXml : IXmlSerializable
  1596. {
  1597. void IXmlSerializable.ReadXml (XmlReader reader)
  1598. {
  1599. }
  1600. void IXmlSerializable.WriteXml (XmlWriter writer)
  1601. {
  1602. }
  1603. XmlSchema IXmlSerializable.GetSchema ()
  1604. {
  1605. return null;
  1606. }
  1607. }
  1608. [DataContract]
  1609. public class ReferenceWrapper
  1610. {
  1611. [DataMember (Order = 1)]
  1612. public ReferenceType T;
  1613. [DataMember (Order = 2)]
  1614. public ReferenceType T2;
  1615. }
  1616. [DataContract (IsReference = true)]
  1617. public class ReferenceType
  1618. {
  1619. [DataMember]
  1620. public string F = "x";
  1621. }
  1622. public class MyList : IList<string>
  1623. {
  1624. List<string> l = new List<string> ();
  1625. public void Clear () { l.Clear (); }
  1626. public void Add(string s) { l.Add (s);}
  1627. public void Insert(int idx, string s) { l.Insert(idx,s);}
  1628. public bool Contains(string s) { return l.Contains(s); }
  1629. public IEnumerator<string> GetEnumerator () { return l.GetEnumerator (); }
  1630. IEnumerator IEnumerable.GetEnumerator () { return l.GetEnumerator (); }
  1631. public bool Remove(string s) { return l.Remove(s); }
  1632. public void RemoveAt(int i) { l.RemoveAt (i);}
  1633. public void CopyTo (string [] arr, int index) { l.CopyTo (arr, index);}
  1634. public int IndexOf (string s) { return l.IndexOf (s); }
  1635. public int Count { get { return l.Count; } }
  1636. public bool IsReadOnly { get { return ((IList<string>) l).IsReadOnly; } }
  1637. public string this [int index] { get { return l [index]; } set { l [index] = value; } }
  1638. }
  1639. [DataContract]
  1640. internal class InterfaceCollectionType
  1641. {
  1642. [DataMember]
  1643. public IList<int> Array { get; set; }
  1644. }
  1645. [DataContract]
  1646. public class NestedContractType
  1647. {
  1648. [DataMember]
  1649. public NestedContractType Nested;
  1650. [DataMember]
  1651. public string X = "x";
  1652. }
  1653. class BaseConstraintType1 // non-serializable
  1654. {
  1655. }
  1656. [Serializable]
  1657. class BaseConstraintType2
  1658. {
  1659. }
  1660. [DataContract]
  1661. class BaseConstraintType3 : BaseConstraintType1
  1662. {
  1663. }
  1664. [DataContract]
  1665. class BaseConstraintType4 : BaseConstraintType2
  1666. {
  1667. }
  1668. [DataContract (Namespace = "urn:foo")]
  1669. public class XmlDocumentContract
  1670. {
  1671. [DataMember (Name = "Content")]
  1672. private XmlElement content;
  1673. public XmlElement Content {
  1674. get { return content; }
  1675. set { content = value; }
  1676. }
  1677. [DataMember (Name = "Nodes")]
  1678. private XmlNode [] nodes;
  1679. public XmlNode [] Nodes {
  1680. get { return nodes; }
  1681. set { nodes = value; }
  1682. }
  1683. }
  1684. }
  1685. [DataContract]
  1686. class GlobalSample1
  1687. {
  1688. }
  1689. [DataContract]
  1690. class Foo<X,Y,Z>
  1691. {
  1692. [DataMember]
  1693. public X Field;
  1694. }
  1695. [CollectionDataContract (Name = "NAME", Namespace = "urn:foo", ItemName = "ITEM", KeyName = "KEY", ValueName = "VALUE")]
  1696. public class MyDictionary<K,V> : Dictionary<K,V>
  1697. {
  1698. }
  1699. // bug #524086
  1700. [DataContract(Namespace="http://example.com/schemas/asx")]
  1701. public class DummyEntry
  1702. {
  1703. [DataMember]
  1704. public DummyEntryInfo EntryInfo { get; set; }
  1705. [DataMember]
  1706. public string Href { get; set; }
  1707. }
  1708. [DataContract(Namespace="http://example.com/schemas/asx"),
  1709. KnownType(typeof(PartDummyEntryInfo))]
  1710. public abstract class DummyEntryInfo
  1711. {
  1712. }
  1713. [DataContract(Namespace="http://example.com/schemas/asx")]
  1714. public class DummyPlaylist
  1715. {
  1716. public IList<DummyEntry> entries = new List<DummyEntry> ();
  1717. [DataMember]
  1718. public IList<DummyEntry> Entries { get { return entries; } set {entries = value;} }
  1719. }
  1720. [DataContract(Namespace="http://example.com/schemas/asx")]
  1721. public class PartDummyEntryInfo : DummyEntryInfo
  1722. {
  1723. public PartDummyEntryInfo() {}
  1724. }
  1725. // bug #524088
  1726. [DataContract(Namespace="http://example.com/schemas/asx")]
  1727. public class AsxEntryInfo
  1728. {
  1729. [DataMember]
  1730. public string AdvertPrompt { get; set; }
  1731. }
  1732. // bug #560155
  1733. [DataContract]
  1734. public class Person
  1735. {
  1736. [DataMember]
  1737. readonly public string name;
  1738. [DataMember]
  1739. readonly public Guid Id = Guid.Empty;
  1740. public Person (string nameIn, Guid idIn)
  1741. {
  1742. name = nameIn;
  1743. Id = idIn;
  1744. }
  1745. public override string ToString()
  1746. {
  1747. return string.Format ("name={0},id={1}", name, Id);
  1748. }
  1749. }
  1750. // bug #599889
  1751. namespace U2U.DataContracts
  1752. {
  1753. [DataContract]
  1754. public class Person
  1755. {
  1756. [DataMember]
  1757. public string Name { get; set; }
  1758. [DataMember]
  1759. public Job Occupation { get; set; }
  1760. }
  1761. [DataContract]
  1762. public class Job
  1763. {
  1764. [DataMember]
  1765. public string Description { get; set; }
  1766. }
  1767. }
  1768. #region bug #610036
  1769. //parent class with a name property
  1770. [DataContract (Namespace = "Company.Foo")]
  1771. public abstract class ParentClass
  1772. {
  1773. //constructor
  1774. public ParentClass (string name)
  1775. {
  1776. Name = name;
  1777. }
  1778. //the name
  1779. [DataMember]
  1780. public string Name{ get; set; }
  1781. }
  1782. //root object
  1783. [DataContract (Namespace = "Company.Foo")]
  1784. public class Root : ParentClass
  1785. {
  1786. //dict
  1787. [DataMember]
  1788. public Dict<Foo> FDict;
  1789. //constructor
  1790. public Root (string name)
  1791. : base (name)
  1792. {
  1793. FDict = new Dict<Foo> ();
  1794. }
  1795. }
  1796. //subclass
  1797. [DataContract (Namespace = "Company.Foo")]
  1798. public class Foo : ParentClass
  1799. {
  1800. //here is one dict
  1801. [DataMember]
  1802. public Dict<Bar> FDict;
  1803. //constructor
  1804. public Foo (string name)
  1805. : base (name)
  1806. {
  1807. FDict = new Dict<Bar> ();
  1808. }
  1809. }
  1810. //another sublass
  1811. [DataContract (Namespace = "Company.Foo")]
  1812. public class Bar : ParentClass
  1813. {
  1814. //constructor
  1815. public Bar (string name)
  1816. : base (name)
  1817. {
  1818. }
  1819. }
  1820. //the custom dictionary
  1821. [CollectionDataContract (ItemName = "DictItem", Namespace = "Company.Foo")]
  1822. public class Dict<T> : Dictionary<string, T> where T : ParentClass
  1823. {
  1824. public void Add (T item)
  1825. {
  1826. Add (item.Name, item);
  1827. }
  1828. }
  1829. [DataContract (IsReference = true)]
  1830. public class Parent
  1831. {
  1832. //constructor
  1833. public Parent ()
  1834. {
  1835. Child = new Child (this);
  1836. }
  1837. [DataMember]
  1838. public Child Child;
  1839. }
  1840. [DataContract]
  1841. public class Child
  1842. {
  1843. public Child ()
  1844. {
  1845. }
  1846. public Child (Parent parent)
  1847. {
  1848. this.Parent = parent;
  1849. }
  1850. [DataMember]
  1851. public Parent Parent;
  1852. }
  1853. namespace SLProto5
  1854. {
  1855. public class CashAmount : Amount
  1856. {
  1857. }
  1858. [DataContract]
  1859. public class CashAmountDC : AmountDC
  1860. {
  1861. }
  1862. public class Amount
  1863. {
  1864. public string Currency { get; set; }
  1865. public string Description { get; set; }
  1866. }
  1867. [DataContract]
  1868. public class AmountDC
  1869. {
  1870. [DataMember]
  1871. public string Currency { get; set; }
  1872. [DataMember]
  1873. public string Description { get; set; }
  1874. }
  1875. }
  1876. namespace SLProto5_Different
  1877. {
  1878. public class CashAmount : SLProto5.Amount
  1879. {
  1880. }
  1881. [DataContract]
  1882. public class CashAmountDC : SLProto5.AmountDC
  1883. {
  1884. }
  1885. }
  1886. // bug #7957
  1887. [DataContract(Namespace = "http://sickhead.com/types/Example")]
  1888. public class MyData
  1889. {
  1890. public MyData()
  1891. {
  1892. Data = new Dictionary<int, byte[]> ();
  1893. }
  1894. [DataMember]
  1895. public Guid FirstId { get; set; }
  1896. [DataMember]
  1897. public string SecondId { get; set; }
  1898. [DataMember]
  1899. public Dictionary<int, byte[]> Data { get; set; }
  1900. }
  1901. #endregion