3 // Atsushi Enomoto <atsushi@ximian.com>
5 // Copyright (C) 2011 Novell, Inc. http://www.novell.com
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections.Generic;
33 using System.Reflection;
34 using System.Runtime.Serialization;
36 using NUnit.Framework;
38 namespace MonoTests.System.Runtime.Serialization
41 public class KnownTypeAttributeTest
43 void Serialize (object instance)
45 var ds = new DataContractSerializer (instance.GetType ());
46 using (var xw = XmlWriter.Create (TextWriter.Null))
47 ds.WriteObject (xw, instance);
51 public void MethodName ()
53 Serialize (new Data () { X = new Bar () });
57 public void MethodName2 ()
59 Serialize (new Data2 () { X = new Bar () });
63 [ExpectedException (typeof (InvalidDataContractException))]
64 public void MethodName3 ()
66 Serialize (new Data3 () { X = new Bar () });
70 [ExpectedException (typeof (InvalidDataContractException))]
71 public void MethodName4 ()
73 Serialize (new Data4 () { X = new Bar () });
77 public void GenericTypeNameArgument ()
79 string expected = "<Foo_KnownTypeAttributeTest.Foo xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns:d1p1='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization' xmlns='urn:foo'><KnownTypeAttributeTest.Foo><d1p1:S>z</d1p1:S></KnownTypeAttributeTest.Foo></Foo_KnownTypeAttributeTest.Foo>".Replace ('\'', '"');
80 var ds = new DataContractSerializer (typeof (MyList<Foo>));
81 var l = new MyList<Foo> ();
82 l.Add (new Foo () { S = "z" });
83 var sw = new StringWriter ();
84 var settings = new XmlWriterSettings () { OmitXmlDeclaration = true };
85 using (var xw = XmlWriter.Create (sw, settings))
86 ds.WriteObject (xw, l);
87 Assert.AreEqual (expected, sw.ToString (), "#1");
91 [ExpectedException (typeof (InvalidDataContractException))]
92 public void DataContractOnCollection ()
94 Serialize (new MyListWrong<Foo> ());
99 public string S { get; set; }
102 public class Bar : Foo
104 public string T { get; set; }
108 [KnownType ("GetTypes")]
112 public Foo X { get; set; }
114 public static IEnumerable<Type> GetTypes ()
116 yield return typeof (Bar);
121 [KnownType ("GetTypes")]
125 public Foo X { get; set; }
127 static IEnumerable<Type> GetTypes () // non-public
129 yield return typeof (Bar);
134 [KnownType ("GetTypes")]
138 public Foo X { get; set; }
140 public IEnumerable<Type> GetTypes () // non-static
142 yield return typeof (Bar);
147 [KnownType ("GetTypes")]
151 public Foo X { get; set; }
153 public static IEnumerable<Type> GetTypes (ICustomAttributeProvider provider) // wrong args
155 yield return typeof (Bar);
159 [CollectionDataContract (Name = "Foo_{0}", Namespace = "urn:foo")]
160 public class MyList<T> : List<T>
164 [DataContract (Name = "Foo_{0}", Namespace = "urn:foo")]
165 public class MyListWrong<T> : List<T>