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