2 // DataContractSerializerTest_ISerializable.cs
5 // Aaron Bockover <abock@xamarin.com>
7 // Copyright 2015 Xamarin Inc. All rights reserved.
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Runtime.Serialization;
33 using NUnit.Framework;
35 namespace MonoTests.System.Runtime.Serialization
38 public class DataContractSerializerTest_ISerializable
41 sealed class TestClassISerializable : ISerializable
43 public string Foo { get; }
45 public TestClassISerializable (string foo)
50 TestClassISerializable (SerializationInfo info, StreamingContext context)
52 Foo = info.GetString ("foo");
55 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
57 info.AddValue ("foo", Foo);
61 // Tests that the ISerializable constructor is properly invoked, which
62 // regressed when integrating MS Reference Source DCS, et. al.:
63 // https://bugzilla.xamarin.com/show_bug.cgi?id=37171
65 public void TestISerializableCtor ()
67 var serializer = new DataContractSerializer (
68 typeof(TestClassISerializable),
69 new DataContractSerializerSettings {
70 DataContractResolver = new Resolver ()
74 var stream = new MemoryStream ();
76 var expected = new TestClassISerializable ("hello world");
77 serializer.WriteObject (stream, expected);
82 var actual = (TestClassISerializable)serializer.ReadObject (stream);
84 Assert.AreEqual (expected.Foo, actual.Foo, "#DCS_ISer_Ctor");
87 // Resolver to force DCS to serialize any type, ensuring the ISerializable
88 // path will be taken for objects implementing that interface
89 class Resolver : DataContractResolver
91 public override Type ResolveName (string typeName, string typeNamespace,
92 Type declaredType, DataContractResolver knownTypeResolver)
94 return Type.GetType (typeNamespace == null
96 : typeNamespace + "." + typeName
100 public override bool TryResolveType (Type type, Type declaredType,
101 DataContractResolver knownTypeResolver,
102 out XmlDictionaryString typeName,
103 out XmlDictionaryString typeNamespace)
105 var name = type.FullName;
106 var namesp = type.Namespace;
107 name = name.Substring (type.Namespace.Length + 1);
108 typeName = new XmlDictionaryString (XmlDictionary.Empty, name, 0);
109 typeNamespace = new XmlDictionaryString (XmlDictionary.Empty, namesp, 0);