Merge pull request #3213 from henricm/fix-for-win-securestring-to-bstr
[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 #if !MOBILE_STATIC
41 using System.Data;
42 #endif
43 using System.IO;
44 using System.Linq;
45 using System.Net;
46 using System.Runtime.Serialization;
47 using System.Text;
48 using System.Xml;
49 using System.Xml.Schema;
50 using System.Xml.Serialization;
51 using NUnit.Framework;
52
53 [assembly: ContractNamespace ("http://www.u2u.be/samples/wcf/2009", ClrNamespace = "U2U.DataContracts")] // bug #599889
54
55 namespace MonoTests.System.Runtime.Serialization
56 {
57         [TestFixture]
58         public class DataContractSerializerTest
59         {
60                 static readonly XmlWriterSettings settings;
61
62                 static DataContractSerializerTest ()
63                 {
64                         settings = new XmlWriterSettings ();
65                         settings.OmitXmlDeclaration = true;
66                 }
67
68                 [DataContract]
69                 class Sample1
70                 {
71                         [DataMember]
72                         public string Member1;
73                 }
74
75                 [Test]
76                 [ExpectedException (typeof (ArgumentNullException))]
77                 public void ConstructorTypeNull ()
78                 {
79                         new DataContractSerializer (null);
80                 }
81
82                 [Test]
83                 public void ConstructorKnownTypesNull ()
84                 {
85                         // null knownTypes is allowed. Though the property is filled.
86                         Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), (IEnumerable<Type>)null).KnownTypes, "#1");
87                         Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), "Foo", String.Empty, null).KnownTypes, "#2");
88                         Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), new XmlDictionary ().Add ("Foo"), XmlDictionaryString.Empty, null).KnownTypes, "#3");
89                 }
90
91                 [Test]
92                 [ExpectedException (typeof (ArgumentNullException))]
93                 public void ConstructorNameNull ()
94                 {
95                         new DataContractSerializer (typeof (Sample1), null, String.Empty);
96                 }
97
98                 [Test]
99                 [ExpectedException (typeof (ArgumentNullException))]
100                 public void ConstructorNamespaceNull ()
101                 {
102                         new DataContractSerializer (typeof (Sample1), "foo", null);
103                 }
104
105                 [Test]
106                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
107                 public void ConstructorNegativeMaxObjects ()
108                 {
109                         new DataContractSerializer (typeof (Sample1), null,
110                                 -1, false, false, null);
111                 }
112
113                 [Test]
114                 public void ConstructorMisc ()
115                 {
116                         new DataContractSerializer (typeof (GlobalSample1));
117                 }
118
119                 [Test]
120                 public void WriteObjectContent ()
121                 {
122                         StringWriter sw = new StringWriter ();
123                         using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
124                                 DataContractSerializer ser =
125                                         new DataContractSerializer (typeof (string));
126                                 xw.WriteStartElement ("my-element");
127                                 ser.WriteObjectContent (xw, "TEST STRING");
128                                 xw.WriteEndElement ();
129                         }
130                         Assert.AreEqual ("<my-element>TEST STRING</my-element>",
131                                 sw.ToString ());
132                 }
133
134                 [Test]
135                 public void WriteObjectToStream ()
136                 {
137                         DataContractSerializer ser =
138                                 new DataContractSerializer (typeof (int));
139                         MemoryStream sw = new MemoryStream ();
140                         ser.WriteObject (sw, 1);
141                         string expected = "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>";
142                         Assert.AreEqual (expected, Encoding.UTF8.GetString (sw.ToArray ()));
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 #if !MOBILE_STATIC
1481                 [Test]
1482                 public void IXmlSerializableCallConstructor ()
1483                 {
1484                         IXmlSerializableCallConstructor  (false);
1485                         IXmlSerializableCallConstructor (true);
1486                 }
1487                 
1488                 void IXmlSerializableCallConstructor (bool binary)
1489                 {
1490                         Stream s = IXmlSerializableCallConstructorSerialize (binary);
1491                         var a = new byte [s.Length];
1492                         s.Position = 0;
1493                         s.Read (a, 0, a.Length);
1494                         s.Position = 0;
1495                         IXmlSerializableCallConstructorDeserialize (s, binary);
1496                 }
1497
1498                 public Stream IXmlSerializableCallConstructorSerialize (bool binary)
1499                 {
1500                         var ds = new DataSet ("ds");
1501                         var dt = new DataTable ("dt");
1502                         ds.Tables.Add (dt);
1503                         dt.Columns.Add ("n", typeof (int));
1504                         dt.Columns.Add ("s", typeof (string));
1505                         
1506                         dt.Rows.Add (5, "five");
1507                         dt.Rows.Add (10, "ten");
1508                         
1509                         ds.AcceptChanges ();
1510                         
1511                         var s = new MemoryStream ();
1512                         
1513                         var w = binary ? XmlDictionaryWriter.CreateBinaryWriter (s) : XmlDictionaryWriter.CreateTextWriter (s);
1514                         
1515                         var x = new DataContractSerializer (typeof (DataSet));
1516                         x.WriteObject (w, ds);
1517                         w.Flush ();
1518         
1519                         return s;
1520                 }
1521                 
1522                 public void IXmlSerializableCallConstructorDeserialize (Stream s, bool binary)
1523                 {
1524                         var r = binary ? XmlDictionaryReader.CreateBinaryReader (s, XmlDictionaryReaderQuotas.Max)
1525                                 : XmlDictionaryReader.CreateTextReader (s, XmlDictionaryReaderQuotas.Max);
1526                         
1527                         var x = new DataContractSerializer (typeof (DataSet));
1528                         
1529                         var ds = (DataSet) x.ReadObject (r);
1530                 }
1531 #endif
1532
1533                 [Test]
1534                 [ExpectedException (typeof (InvalidDataContractException))] // BaseConstraintType1 is neither DataContract nor Serializable.
1535                 public void BaseConstraint1 ()
1536                 {
1537                         new DataContractSerializer (typeof (BaseConstraintType3)).WriteObject (XmlWriter.Create (TextWriter.Null), new BaseConstraintType3 ());
1538                 }
1539
1540                 [Test]
1541                 public void BaseConstraint2 ()
1542                 {
1543                         new DataContractSerializer (typeof (BaseConstraintType4)).WriteObject (XmlWriter.Create (TextWriter.Null), new BaseConstraintType4 ());
1544                 }
1545
1546                 [Test] // bug #652331
1547                 public void MembersNamespacesInBaseType ()
1548                 {
1549                         string xml1 = @"<Currency>JPY</Currency><Description i:nil=""true"" />";
1550                         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"" />";
1551                         Assert.AreEqual ("JPY", MembersNamespacesInBaseType_Part<SLProto5.CashAmount> (new SLProto5.CashAmount () { Currency = "JPY" }, xml1, "#1").Currency, "r#1");
1552                         Assert.AreEqual ("JPY", MembersNamespacesInBaseType_Part<SLProto5_Different.CashAmount> (new SLProto5_Different.CashAmount () { Currency = "JPY" }, xml2, "#2").Currency, "r#2");
1553                         Assert.AreEqual ("JPY", MembersNamespacesInBaseType_Part<SLProto5.CashAmountDC> (new SLProto5.CashAmountDC () { Currency = "JPY" }, xml1, "#3").Currency, "r#3");
1554                         Assert.AreEqual ("JPY", MembersNamespacesInBaseType_Part<SLProto5_Different.CashAmountDC> (new SLProto5_Different.CashAmountDC () { Currency = "JPY" }, xml2, "#4").Currency, "r#4");
1555                 }
1556
1557                 T MembersNamespacesInBaseType_Part<T> (T instance, string expectedPart, string assert)
1558                 {
1559                         var ds = new DataContractSerializer (typeof (T));
1560                         var sw = new StringWriter ();
1561                         using (var w = XmlWriter.Create (sw))
1562                                 ds.WriteObject (w, instance);
1563                         Assert.IsTrue (sw.ToString ().IndexOf (expectedPart) > 0, assert);
1564                         return (T) ds.ReadObject (XmlReader.Create (new StringReader (sw.ToString ())));
1565                 }
1566
1567                 [Test]
1568                 public void DateTimeOffsetSerialization ()
1569                 {
1570                         var ds = new DataContractSerializer (typeof (DateTimeOffset));
1571                         var sw = new StringWriter ();
1572                         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 ('\'', '"');
1573                                                 
1574                         var v = new DateTimeOffset (new DateTime (2011, 3, 1, 4, 5, 6, 78), TimeSpan.FromMinutes (120));
1575                         using (var xw = XmlWriter.Create (sw, settings)) {
1576                                 ds.WriteObject (xw, v);
1577                         }
1578                         Assert.AreEqual (xml, sw.ToString (), "#1");
1579                         Assert.AreEqual (v, ds.ReadObject (XmlReader.Create (new StringReader (sw.ToString ()))), "#2");
1580                 }
1581                 
1582                 [Test]
1583                 public void DateTimeOffsetNullableSerialization ()
1584                 {
1585                         var ds = new DataContractSerializer (typeof (DateTimeOffset?));
1586                         var sw = new StringWriter ();
1587                         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>";
1588                         
1589                         var v = new DateTimeOffset (new DateTime (2012, 05, 04, 02, 34, 0), TimeSpan.FromMinutes (120));
1590                         using (var xw = XmlWriter.Create (sw, settings)) {
1591                                 ds.WriteObject (xw, v);
1592                         }
1593                         Assert.AreEqual (xml, sw.ToString (), "#1");
1594                         Assert.AreEqual (v, ds.ReadObject (XmlReader.Create (new StringReader (sw.ToString ()))), "#2");
1595                 }
1596
1597                 [Test]
1598                 public void XmlDocumentSupport ()
1599                 {
1600                         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 ('\'', '"');
1601                         var xml2 = "<Root>Hello, world!</Root>";
1602                         var obj = new XmlDocumentContract ();
1603                         var doc = new XmlDocument ();
1604                         doc.LoadXml (xml2);
1605                         obj.Content = doc.DocumentElement;
1606                         doc = new XmlDocument ();
1607                         doc.LoadXml ("<root><child1/><child2/></root>");
1608                         var l = new List<XmlNode> ();
1609                         foreach (XmlNode node in doc.DocumentElement.ChildNodes)
1610                                 l.Add (node);
1611                         obj.Nodes = l.ToArray ();
1612                         var serializer = new DataContractSerializer (typeof (XmlDocumentContract))
1613                         ;
1614                         var sb = new StringBuilder ();
1615                         using (var writer = new StringWriter (sb))
1616                                 serializer.WriteObject (new XmlTextWriter (writer), obj);
1617                         Assert.AreEqual (xml, sb.ToString (), "#1");
1618                         using (var reader = new StringReader (sb.ToString ()))
1619                                 obj = serializer.ReadObject (new XmlTextReader (reader)) as XmlDocumentContract;
1620                         Assert.AreEqual ("Hello, world!", obj.Content != null ? obj.Content.InnerText : String.Empty, "#2");
1621                         Assert.AreEqual (2, obj.Nodes != null ? obj.Nodes.Length : -1, "#3");
1622                 }
1623
1624                 [Test]
1625                 public void ArrayAsEnumerableAsRoot ()
1626                 {
1627                         var ds = new DataContractSerializer (typeof (IEnumerable<Guid>));
1628                         var sw = new StringWriter ();
1629                         using (var xw = XmlWriter.Create (sw, settings))
1630                                 ds.WriteObject (xw, new Guid [] {Guid.Empty});
1631                         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 ('\'', '"');
1632                         Assert.AreEqual (xml, sw.ToString (), "#1");
1633                 }
1634                 
1635                 // bug #7957
1636                 [Test]
1637                 public void DeserializeEmptyDictionary ()
1638                 {
1639                         string whatItGets = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
1640                                 + "<MyData xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://sickhead.com/types/Example\">"
1641                                         + "<Data xmlns:b=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\"/>"
1642                                         + "<FirstId>b8a7eb6f-f593-4668-8178-07be9f7266d1</FirstId>"
1643                                         + "<SecondId>ID-GOES-HERE</SecondId>"
1644                                         + "</MyData>";
1645                         var serializer = new DataContractSerializer (typeof (MyData));
1646                         using (var stream = new MemoryStream (Encoding.UTF8.GetBytes (whatItGets.Replace ("ID-GOES-HERE", Guid.NewGuid ().ToString ()))))
1647                         {
1648                                 var data = serializer.ReadObject (stream);
1649                         }
1650                 }
1651         }
1652         
1653         [DataContract]
1654         public class MemberIgnored
1655         {
1656                 [DataMember]
1657                 MemberIgnoredBody body = new MemberIgnoredBody ();
1658         }
1659
1660         public class MemberIgnoredBody
1661         {
1662                 [IgnoreDataMember]
1663                 public string Foo = "foo";
1664
1665                 public string Bar = "bar";
1666         }
1667
1668         public enum Colors {
1669                 Red, Green, Blue
1670         }
1671
1672         [Flags]
1673         public enum Colors2 {
1674                 Red, Green, Blue
1675         }
1676
1677         [DataContract (Name = "_ColorsWithDC")]
1678         public enum ColorsWithDC {
1679
1680                 [EnumMember (Value = "_Red")]
1681                 Red, 
1682                 [EnumMember]
1683                 Green, 
1684                 Blue
1685         }
1686
1687
1688         public enum ColorsEnumMemberNoDC {
1689                 [EnumMember (Value = "_Red")]
1690                 Red, 
1691                 [EnumMember]
1692                 Green, 
1693                 Blue
1694         }
1695
1696         [DataContract]
1697         public class DCWithEnum {
1698                 [DataMember (Name = "_colors")]
1699                 public Colors colors;
1700         }
1701
1702         [DataContract]
1703         public class DCWithTwoEnums {
1704                 [DataMember]
1705                 public Colors colors;
1706                 [DataMember]
1707                 public Colors colors2;
1708         }
1709
1710         [DataContract]
1711         public class DCWithNullableEnum {
1712                 [DataMember]
1713                 public Colors? colors;
1714         }
1715
1716
1717         [DataContract (Namespace = "Base")]
1718         public class BaseDC {
1719                 [DataMember]
1720                 public int baseVal;
1721         }
1722
1723         [DataContract (Namespace = "Derived")]
1724         public class DerivedDC : BaseDC {
1725                 [DataMember]
1726                 public int derivedVal;
1727         }
1728
1729         [DataContract]
1730         public class NestedDC {
1731                 public NestedDC (string name) { this.Name = name; }
1732
1733                 [DataMember]
1734                 public string Name;
1735         }
1736
1737         [DataContract]
1738         public class NestingDC {
1739                 [DataMember]
1740                 public NestedDC Field1;
1741                 [DataMember]
1742                 public NestedDC Field2;
1743         }
1744
1745         [DataContract (Namespace = "test1")]
1746         public class NestedDC2 {
1747                 public NestedDC2 (string name) { this.Name = name; }
1748
1749                 [DataMember]
1750                 public string Name;
1751         }
1752
1753         [DataContract (Namespace = "test2")]
1754         public class NestingDC2 {
1755                 [DataMember]
1756                 public NestedDC2 Field;
1757         }
1758
1759         [DataContract]
1760         public class DCEmpty
1761         {
1762                 // serializer doesn't touch it.
1763                 public string Foo = "TEST";
1764         }
1765
1766         [DataContract (Namespace = "")]
1767         public class DCEmptyNoNS
1768         {
1769         }
1770
1771         [DataContract]
1772         public class DCSimple1
1773         {
1774                 [DataMember]
1775                 public string Foo = "TEST";
1776         }
1777
1778         [DataContract]
1779         public class DCHasNonDC
1780         {
1781                 [DataMember]
1782                 public NonDC Hoge= new NonDC ();
1783         }
1784
1785         public class NonDC
1786         {
1787                 public string Whee = "whee!";
1788         }
1789
1790         [DataContract]
1791         public class DCHasSerializable
1792         {
1793                 [DataMember]
1794                 public SimpleSer1 Ser = new SimpleSer1 ();
1795         }
1796
1797         [DataContract (Name = "Foo")]
1798         public class DCWithName
1799         {
1800                 [DataMember (Name = "FooMember")]
1801                 public string DMWithName = "value";
1802         }
1803
1804         [DataContract (Name = "")]
1805         public class DCWithEmptyName
1806         {
1807         }
1808
1809         [DataContract (Name = null)]
1810         public class DCWithNullName
1811         {
1812         }
1813
1814         [DataContract (Namespace = "")]
1815         public class DCWithEmptyNamespace
1816         {
1817         }
1818
1819         [Serializable]
1820         public class SimpleSer1
1821         {
1822                 public string Doh = "doh!";
1823                 [NonSerialized]
1824                 public string Bah = "bah!";
1825         }
1826
1827         public class Wrapper
1828         {
1829                 [DataContract]
1830                 public class DCWrapped
1831                 {
1832                 }
1833         }
1834
1835         [DataContract]
1836         public class CollectionContainer
1837         {
1838                 Collection<string> items = new Collection<string> ();
1839
1840                 [DataMember]
1841                 public Collection<string> Items {
1842                         get { return items; }
1843                 }
1844         }
1845
1846         [CollectionDataContract]
1847         public class DataCollection<T> : Collection<T>
1848         {
1849         }
1850
1851         [DataContract]
1852         public class DataCollectionContainer
1853         {
1854                 DataCollection<string> items = new DataCollection<string> ();
1855
1856                 [DataMember]
1857                 public DataCollection<string> Items {
1858                         get { return items; }
1859                 }
1860         }
1861
1862         [DataContract]
1863         class SerializeNonDCArrayType
1864         {
1865                 [DataMember]
1866                 public NonDCItem [] IPAddresses = new NonDCItem [0];
1867         }
1868
1869         public class NonDCItem
1870         {
1871                 public int [] Data { get; set; }
1872         }
1873
1874         [DataContract]
1875         public class VerifyObjectNameTestData
1876         {
1877                 [DataMember]
1878                 string Member1 = "foo";
1879
1880                 public string GetMember() { return Member1; }
1881         }
1882
1883         [XmlRoot(ElementName = "simple", Namespace = "")]
1884         public class SimpleXml : IXmlSerializable 
1885         {
1886                 void IXmlSerializable.ReadXml (XmlReader reader)
1887                 {
1888                 }
1889
1890                 void IXmlSerializable.WriteXml (XmlWriter writer)
1891                 {
1892                 }
1893
1894                 XmlSchema IXmlSerializable.GetSchema ()
1895                 {
1896                         return null;
1897                 }
1898
1899         }
1900
1901         [DataContract]
1902         public class ReferenceWrapper
1903         {
1904                 [DataMember (Order = 1)]
1905                 public ReferenceType T;
1906
1907                 [DataMember (Order = 2)]
1908                 public ReferenceType T2;
1909         }
1910
1911         [DataContract (IsReference = true)]
1912         public class ReferenceType
1913         {
1914                 [DataMember]
1915                 public string F = "x";
1916         }
1917
1918         public class MyList : IList<string>
1919         {
1920                 List<string> l = new List<string> ();
1921                 public void Clear () { l.Clear (); }
1922                 public void Add(string s) { l.Add (s);}
1923                 public void Insert(int idx, string s) { l.Insert(idx,s);}
1924                 public bool Contains(string s) { return l.Contains(s); }
1925                 public IEnumerator<string> GetEnumerator () { return l.GetEnumerator (); }
1926                 IEnumerator IEnumerable.GetEnumerator () { return l.GetEnumerator (); }
1927                 public bool Remove(string s) { return l.Remove(s); }
1928                 public void RemoveAt(int i) { l.RemoveAt (i);}
1929                 public void CopyTo (string [] arr, int index) { l.CopyTo (arr, index);}
1930                 public int IndexOf (string s) { return l.IndexOf (s); }
1931         
1932                 public int Count { get { return l.Count; } }
1933                 public bool IsReadOnly { get { return ((IList<string>) l).IsReadOnly; } }
1934                 public string this [int index] { get { return l [index]; } set { l [index] = value; } }
1935         }
1936
1937         [DataContract]
1938         internal class InterfaceCollectionType
1939         {
1940                 [DataMember]
1941                 public IList<int> Array { get; set; }
1942         }
1943
1944         [DataContract]
1945         public class NestedContractType
1946         {
1947                 [DataMember]
1948                 public NestedContractType Nested;
1949                 [DataMember]
1950                 public string X = "x";
1951         }
1952
1953         class BaseConstraintType1 // non-serializable
1954         {
1955         }
1956         
1957         [Serializable]
1958         class BaseConstraintType2
1959         {
1960         }
1961         
1962         [DataContract]
1963         class BaseConstraintType3 : BaseConstraintType1
1964         {
1965         }
1966         
1967         [DataContract]
1968         class BaseConstraintType4 : BaseConstraintType2
1969         {
1970         }
1971
1972         [DataContract (Namespace = "urn:foo")]
1973         public class XmlDocumentContract
1974         {
1975                 [DataMember (Name = "Content")]
1976                 private XmlElement content;
1977
1978                 public XmlElement Content {
1979                         get { return content; }
1980                         set { content = value; }
1981                 }
1982
1983                 [DataMember (Name = "Nodes")]
1984                 private XmlNode [] nodes;
1985
1986                 public XmlNode [] Nodes {
1987                         get { return nodes; }
1988                         set { nodes = value; }
1989                 }
1990         }
1991 }
1992
1993 [DataContract]
1994 class GlobalSample1
1995 {
1996 }
1997
1998 [DataContract]
1999 class Foo<X,Y,Z>
2000 {
2001         [DataMember]
2002         public X Field;
2003 }
2004
2005 [CollectionDataContract (Name = "NAME", Namespace = "urn:foo", ItemName = "ITEM", KeyName = "KEY", ValueName = "VALUE")]
2006 public class MyDictionary<K,V> : Dictionary<K,V>
2007 {
2008 }
2009
2010 // bug #524086
2011 [DataContract(Namespace="http://example.com/schemas/asx")]
2012 public class DummyEntry
2013 {
2014     [DataMember]
2015     public DummyEntryInfo EntryInfo { get; set; }
2016     [DataMember]
2017     public string Href { get; set; }
2018 }
2019
2020 [DataContract(Namespace="http://example.com/schemas/asx"),
2021 KnownType(typeof(PartDummyEntryInfo))]
2022 public abstract class DummyEntryInfo
2023 {
2024 }
2025
2026 [DataContract(Namespace="http://example.com/schemas/asx")]
2027 public class DummyPlaylist
2028 {
2029     public IList<DummyEntry> entries = new List<DummyEntry> ();
2030
2031     [DataMember]
2032     public IList<DummyEntry> Entries { get { return entries; } set {entries = value;} }
2033 }
2034
2035 [DataContract(Namespace="http://example.com/schemas/asx")]
2036 public class PartDummyEntryInfo : DummyEntryInfo
2037 {
2038     public PartDummyEntryInfo() {}
2039 }
2040
2041 // bug #524088
2042
2043 [DataContract(Namespace="http://example.com/schemas/asx")]
2044 public class AsxEntryInfo
2045 {
2046     [DataMember]
2047     public string AdvertPrompt { get; set; }
2048 }
2049
2050 // bug #560155
2051
2052 [DataContract]
2053 public class Person
2054 {
2055         [DataMember]
2056         readonly public string name;
2057         [DataMember]
2058         readonly public Guid Id = Guid.Empty;
2059
2060         public Person (string nameIn, Guid idIn)
2061         {
2062                 name = nameIn;
2063                 Id = idIn;
2064         }
2065
2066         public override string ToString()
2067         {
2068                 return string.Format ("name={0},id={1}", name, Id);
2069         }
2070 }
2071
2072 // bug #599889
2073 namespace U2U.DataContracts
2074 {
2075         [DataContract]
2076         public class Person
2077         {
2078                 [DataMember]
2079                 public string Name { get; set; }
2080
2081                 [DataMember]
2082                 public Job Occupation { get; set; }
2083         }
2084
2085         [DataContract]
2086         public class Job
2087         {
2088                 [DataMember]
2089                 public string Description { get; set; }
2090         }
2091 }
2092
2093 #region bug #610036
2094 //parent class with a name property
2095 [DataContract (Namespace = "Company.Foo")]
2096 public abstract class ParentClass
2097 {
2098         
2099         //constructor
2100         public ParentClass (string name)
2101         {
2102                 Name = name;
2103         }
2104         
2105         //the name
2106         [DataMember]
2107         public string Name{ get; set; }
2108
2109 }
2110
2111 //root object
2112 [DataContract (Namespace = "Company.Foo")]
2113 public class Root : ParentClass
2114 {
2115         //dict
2116         [DataMember]
2117         public Dict<Foo> FDict; 
2118         
2119         //constructor
2120         public Root (string name)
2121                 : base (name)
2122         {
2123                 FDict = new Dict<Foo> ();
2124         }
2125 }
2126
2127
2128 //subclass
2129 [DataContract (Namespace = "Company.Foo")]
2130 public class Foo : ParentClass
2131 {
2132         //here is one dict
2133         [DataMember]
2134         public Dict<Bar> FDict;
2135         
2136         //constructor
2137         public Foo (string name) 
2138                 : base (name)
2139         {
2140                 FDict = new Dict<Bar> ();
2141         }
2142         
2143 }
2144
2145 //another sublass
2146 [DataContract (Namespace = "Company.Foo")]
2147 public class Bar : ParentClass
2148 {
2149         //constructor
2150         public Bar (string name)
2151                 : base (name)
2152         {
2153         }
2154         
2155 }
2156 //the custom dictionary
2157 [CollectionDataContract (ItemName = "DictItem", Namespace = "Company.Foo")]
2158 public class Dict<T> : Dictionary<string, T> where T : ParentClass
2159 {
2160         public void Add (T item)
2161         {
2162                 Add (item.Name, item);
2163         }
2164         
2165 }
2166
2167 [DataContract (IsReference = true)]
2168 public class Parent
2169 {
2170         //constructor
2171         public Parent ()
2172         {
2173                 Child = new Child (this);
2174         }
2175
2176         [DataMember]
2177         public Child Child;
2178 }
2179
2180 [DataContract]
2181 public class Child
2182 {
2183         public Child ()
2184         {
2185         }
2186         
2187         public Child (Parent parent)
2188         {
2189                 this.Parent = parent;
2190         }
2191         
2192         [DataMember]
2193         public Parent Parent;
2194 }
2195
2196 namespace SLProto5
2197 {
2198         public class CashAmount : Amount
2199         {
2200         }
2201
2202         [DataContract]
2203         public class CashAmountDC : AmountDC
2204         {
2205         }
2206
2207         public class Amount
2208         {
2209                 public string Currency { get; set; }
2210                 public string Description { get; set; }
2211         }
2212
2213         [DataContract]
2214         public class AmountDC
2215         {
2216                 [DataMember]
2217                 public string Currency { get; set; }
2218                 [DataMember]
2219                 public string Description { get; set; }
2220         }
2221 }
2222
2223 namespace SLProto5_Different
2224 {
2225         public class CashAmount : SLProto5.Amount
2226         {
2227         }
2228
2229         [DataContract]
2230         public class CashAmountDC : SLProto5.AmountDC
2231         {
2232         }
2233 }
2234
2235 // bug #7957
2236 [DataContract(Namespace = "http://sickhead.com/types/Example")]
2237 public class MyData
2238 {
2239         public MyData()
2240         {
2241                 Data = new Dictionary<int, byte[]> ();
2242         }
2243
2244         [DataMember]
2245         public Guid FirstId { get; set; }
2246
2247         [DataMember]
2248         public string SecondId { get; set; }
2249
2250         [DataMember]
2251         public Dictionary<int, byte[]> Data { get; set; }
2252 }
2253
2254 #endregion