Skip to content

Example usage

grammophone edited this page Nov 14, 2018 · 2 revisions

Using FastBinaryFormatter is same as built-in .NET serialization formatters.

Album[] serializedObject = BuildAlbumArray(); // Example object graph.

var formatter = new FastBinaryFormatter();

// If required, define surragate selectors.
var surrogateSelector = new SurrogateSelector();

surrogateSelector.AddSurrogate(
	typeof(Genre),
	new StreamingContext(),
	new GenreSerializationSurrogate());

formatter.SurrogateSelector.ChainSelector(surrogateSelector);

using (var stream = new MemoryStream())
{
	// Serialize.
	formatter.Serialize(stream, serializedObject);

	stream.Seek(0, SeekOrigin.Begin);

	// Deserialize.
	var deserializedObject = (Album[])formatter.Deserialize(stream);
}
Clone this wiki locally