// Serialize class with a IBonded<Through> field containing value From. // Deserialize and then deserialize object Through and To from the bonded<Through> field. void PolymorphicDeserialization <From, Through, To, R>( Action <BondClass <IBonded <Through> >, Stream> serialize, Func <Stream, BondClass <IBonded <Through> > > deserialize) where From : class, Through, new() where Through : class, new() where To : class, new() where R : ICloneable <R> { var field = Random.Init <From>(); var from = new BondClass <IBonded <Through> >(); from.field = new Bonded <From>(field); var stream = new MemoryStream(); serialize(from, stream); stream.Position = 0; var to = deserialize(stream); Assert.IsTrue(field.IsEqual(to.field.Deserialize())); Assert.IsTrue(field.IsEqual(to.field.Deserialize <To>())); Assert.IsTrue(field.IsEqual(to.field.Convert <To>().Deserialize())); // bonded<T> for untagged protocol in not serialized using the protocol R // but instead is marshaled using a tagged protocol. if (!typeof(IUntaggedProtocolReader).IsAssignableFrom(typeof(R))) { var deserializer = new Deserializer <R>(typeof(To), Schema <From> .RuntimeSchema); Assert.IsTrue(field.IsEqual(deserializer.Deserialize(to.field.Convert <To>()))); } }
// Serialize a class with IBonded<From> field an deserialize into class with non-lazy struct To field. void NonLazyDeserialization <From, To>( Action <BondClass <IBonded <From> >, Stream> serialize, Func <Stream, BondClass <To> > deserialize) where From : class, new() where To : class { var field = Random.Init <From>(); var from = new BondClass <IBonded <From> >(); from.field = new Bonded <From>(field); var stream = new MemoryStream(); serialize(from, stream); stream.Position = 0; var to = deserialize(stream); Assert.IsTrue(field.IsEqual(to.field)); }