public void Polymorphism() { // As of 0.7, polymorphism is implemented. // Setup the context to use map for pretty printing. var context = new SerializationContext { SerializationMethod = SerializationMethod.Map }; var serializer = context.GetSerializer <PolymorphicHolder>(); var rootObject = new PolymorphicHolder { WithRuntimeType = new FileObject { Path = "/path/to/file" }, WithKnownType = new DirectoryObject { Path = "/path/to/dir/" } }; using (var buffer = new MemoryStream()) { serializer.Pack( buffer, new PolymorphicHolder { WithRuntimeType = new FileObject { Path = "/path/to/file" }, WithKnownType = new DirectoryObject { Path = "/path/to/dir/" } } ); buffer.Position = 0; Print(buffer); buffer.Position = 0; var deserialized = serializer.Unpack(buffer); Assert.That(deserialized.WithRuntimeType, Is.TypeOf <FileObject>()); Assert.That(deserialized.WithRuntimeType.Path, Is.EqualTo("/path/to/file")); Assert.That(deserialized.WithKnownType, Is.TypeOf <DirectoryObject>()); Assert.That(deserialized.WithKnownType.Path, Is.EqualTo("/path/to/dir/")); } }
public void Polymorphism() { // As of 0.7, polymorphism is implemented. // Setup the context to use map for pretty printing. var context = new SerializationContext { SerializationMethod = SerializationMethod.Map }; var serializer = context.GetSerializer<PolymorphicHolder>(); var rootObject = new PolymorphicHolder { WithRuntimeType = new FileObject { Path = "/path/to/file" }, WithKnownType = new DirectoryObject { Path = "/path/to/dir/" } }; using ( var buffer = new MemoryStream() ) { serializer.Pack( buffer, new PolymorphicHolder { WithRuntimeType = new FileObject { Path = "/path/to/file" }, WithKnownType = new DirectoryObject { Path = "/path/to/dir/" } } ); buffer.Position = 0; Print( buffer ); buffer.Position = 0; var deserialized = serializer.Unpack( buffer ); Assert.That( deserialized.WithRuntimeType, Is.TypeOf<FileObject>() ); Assert.That( deserialized.WithRuntimeType.Path, Is.EqualTo( "/path/to/file" ) ); Assert.That( deserialized.WithKnownType, Is.TypeOf<DirectoryObject>() ); Assert.That( deserialized.WithKnownType.Path, Is.EqualTo( "/path/to/dir/" ) ); } }