If all the types have been marked with the [Serializable()] tag, then
the following is the simplest approach:

private MyType _MyObject;

private void PersistToFile() {
    BinaryFormatter binaryformatter = new BinaryFormatter();
    _FileStream.Position = 0;
    binaryformatter.Serialize( _FileStream, _MyObject );
    _FileStream.Flush();
}

private void LoadFromFile() {
    BinaryFormatter binaryformatter = new BinaryFormatter();
    _FileStream.Position = 0;
    _MyObject = binaryformatter.Deserialize( _FileStream ) as MyType;
}

It will be saved as a binary blob which means that you will encounter
versioning issues if the assemblies of the objects being serialized have
a version or a non-implementation change (member changes, etc.).  This
can be mitigated by creating a cross version converter class (rather
than casting as "MyType" you make a call to a function that does nothing
but cast and assign the deserialized object to "_MyObject", of which
there are two overloads, one for the old version (which does the
conversion from the old version of the object to a new instance of the
new version before assigning that to "_MyObject") and the other for the
new version (which only casts and assigns).  You should be able to
google for the particulars of setting up namespaces for assembly
versions and then using different versions of an object in the same
code.

Erik

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Knowlton,
Gerald F.
Sent: Monday, March 05, 2007 6:54 AM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: Saving the contents of a collection of objects...

Hello all,

     I am looking for a solution to saving the contents of a collection
of objects to disk and then reading the data back into the collection.

        Suppose I have this collection called "Col" and each item is an
object called "ID". The ID object has two properties called "Name" and
"Address". The Col has 1 ... n number of ID objects. What I would like
to do is save the entire collection to a disk file and at some later
time, retrieve the values back into the collection. Naturally, the
program stops after the save and then, perhaps, the next day it is
started up again.=20

      I think the Serialization attribute plays a big part in this, but
frankly I don't know how to use that attribute nor be able to read the
serialized data back in.=20

      May I ask for someone to point me to some possible on line
solutions or if this is relatively simple, show me how it might be done?


Thank you very much.


Best regards,


Jerry Knowlton

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
This list is hosted by DevelopMentor=AE  http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to