X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FTest%2FSystem.Runtime.Serialization.Formatters.Binary%2FBinaryFormatterTest.cs;h=6d4b997fb45317b67a5c928e23f97d46332aa4e8;hb=e82dc02e27ea872f3f1bd19d8a6c9770ae12716d;hp=7cf781c6531b3e48b53e4ff2a2a4495bca990482;hpb=912dda4613db82b1983753b258a72848d3bdf025;p=mono.git diff --git a/mcs/class/corlib/Test/System.Runtime.Serialization.Formatters.Binary/BinaryFormatterTest.cs b/mcs/class/corlib/Test/System.Runtime.Serialization.Formatters.Binary/BinaryFormatterTest.cs index 7cf781c6531..6d4b997fb45 100644 --- a/mcs/class/corlib/Test/System.Runtime.Serialization.Formatters.Binary/BinaryFormatterTest.cs +++ b/mcs/class/corlib/Test/System.Runtime.Serialization.Formatters.Binary/BinaryFormatterTest.cs @@ -62,6 +62,45 @@ namespace MonoTests.System.Runtime.Serialization.Formatters.Binary } } + namespace NestedA + { + [Serializable] + public class QualifiedFieldTest + { + int value = 0; + + public int ValueA { + get { return value; } + set { this.value = value; } + } + } + } + + namespace NestedB + { + [Serializable] + public class QualifiedFieldTest : NestedA.QualifiedFieldTest + { + int value = 0; + + public int ValueB { + get { return value; } + set { this.value = value; } + } + } + } + + [Serializable] + public class QualifiedFieldTest : NestedB.QualifiedFieldTest + { + int value = 0; + + public int Value { + get { return value; } + set { this.value = value; } + } + } + class SurrogateSelector: ISurrogateSelector { public void ChainSelector (ISurrogateSelector selector) @@ -452,6 +491,23 @@ namespace MonoTests.System.Runtime.Serialization.Formatters.Binary return ms; } + [Test] + public void QualifiedField() + { + QualifiedFieldTest a = new QualifiedFieldTest (); + a.ValueA = 1; + a.ValueB = 2; + a.Value = 3; + Stream ms = new MemoryStream (); + BinaryFormatter bf = new BinaryFormatter (); + bf.Serialize(ms, a); + ms.Position = 0; + QualifiedFieldTest b = (QualifiedFieldTest)bf.Deserialize (ms); + Assert.AreEqual (a.ValueA, b.ValueA, "#1"); + Assert.AreEqual (a.ValueB, b.ValueB, "#2"); + Assert.AreEqual (a.Value, b.Value, "#3"); + } + #if NET_4_0 [Test] public void SerializationBindToName () @@ -603,6 +659,68 @@ namespace MonoTests.System.Runtime.Serialization.Formatters.Binary info.AddValue ("Id", Id); } } + + [Serializable] + public class OtherClass + { + } + + [Serializable] + public class BaseClass + { + public OtherClass Other { get; set; } + } + + public class CustomSerBinder: SerializationBinder + { + public override void BindToName (Type serializedType, out string assemblyName, out string typeName) + { + assemblyName = null; + if (serializedType == typeof (BaseClass)) + typeName = "base"; + else if (serializedType == typeof (OtherClass)) + typeName = "other"; + else + throw new ArgumentException ("Unknown type", "serializedType"); + } + + public override Type BindToType (string assemblyName, string typeName) + { + switch (typeName) { + case "base": + return typeof (BaseClass); + case "other": + return typeof (OtherClass); + default: + throw new ArgumentException ("Unknown type name", "typeName"); + } + } + } + + [Test] + public void BinderShouldBeUsedForProperties () + { + using (var serStream = new MemoryStream ()) { + var binder = new CustomSerBinder (); + + // serialize + var original = new BaseClass () { + Other = new OtherClass () + }; + var formatter = new BinaryFormatter (); + formatter.Binder = binder; + formatter.Serialize (serStream, original); + + // deserialize, making sure we're using a new formatter, just to be thorough + formatter = new BinaryFormatter (); + formatter.Binder = binder; + serStream.Seek (0, SeekOrigin.Begin); + var deserialized = formatter.Deserialize (serStream); + + Assert.AreEqual (original.GetType (), deserialized.GetType ()); + Assert.AreEqual (original.Other.GetType (), ((BaseClass)deserialized).Other.GetType ()); + } + } #endif } }