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