I’ve got a custom collection which adds functionality to the ‘ArrayList’ class.
Here’s some code from the class:
[Serializable]
class Coll : ArrayList
{
public void Save(string path)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream fsOut = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
formatter.Serialize(fsOut, this);
fsOut.Dispose();
}
}
I’m now trying to deserialize a file, and fill the collection with the contents of the file. Basically the opposite of my Save(string path) method.
This is what I’ve got so far:
public void Read(string path)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream fsIn = new FileStream(path, FileMode.Open, FileAccess.Read);
formatter.Deserialize(fsIn);
fsIn.Dispose();
}
How should I go about populating the collection with the contents which has been deserialized?
BinaryFormatterdoesn’t support serialising into an existing object. You could deserialize it into a new list – just make it astaticmethod and return the value.Other thoughts:
ArrayListunless you are in .net 1.1:List<T>would be preferableBinaryFormatterfor this… Or anything else really