Home
Products
Support
Blog
Resources
Company
NMath Matrix User's Guide
TOC |  Previous |  Next |  Index

8.1 Binary Serialization

The System.Runtime.Serialization.Formatters.Binary.BinaryFormatter class provides Serialize() and Deserialize() methods for persisting an object in binary format to a given stream. For example, this code serializes two DoubleBandMatrix objects to a file:

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

int rows = 20, cols = 12, ub = 2, lb = 1;
DoubleVector data = new DoubleVector( (ub+lb+1)*cols, 1, 1 );
DoubleBandMatrix A =
   new DoubleBandMatrix( data, rows, cols, lb, ub );

rows = 34;
cols = 38;
ub = 2;
lb = 2;
data = new DoubleVector( (ub+lb+1)*cols, 1, 1 );
DoubleBandMatrix B =
   new DoubleBandMatrix( data, rows, cols, lb, ub );


FileStream binStream = File.Create( "myData.dat" );
BinaryFormatter binFormatter = new BinaryFormatter();

binFormatter.Serialize( binStream, A );
binFormatter.Serialize( binStream, B );
binStream.Close();

This code restores the objects from the file:

binStream = File.OpenRead( "myData.dat" );

DoubleBandMatrix A2 =
  (DoubleBandMatrix)binFormatter.Deserialize( binStream );
DoubleBandMatrix B2 =
  (DoubleBandMatrix)binFormatter.Deserialize( binStream );

binStream.Close();
File.Delete( "myData.dat" );

TOC |  Previous |  Next |  Index

Copyright © 2008 CenterSpace Software, LLC. All rights reserved.
All trademarks and registered trademarks mentioned on this web site are the property of their respective owners.
Contact Webmaster