I have a button on my main form which calls a method to serialize some objects to disk. I am trying to add these objects to an ArrayList and then serialize them using a BinaryFormatter and a FileStream.
public void SerializeGAToDisk(string GenAlgName)
{
// Let's make a list that includes all the objects we
// need to store a GA instance
ArrayList GAContents = new ArrayList();
GAContents.Add(GenAlgInstances[GenAlgName]); // Structure and info for a GA
GAContents.Add(RunningGAs[GenAlgName]); // There may be several running GA's
using (FileStream fStream = new FileStream(GenAlgName + ".ga",
FileMode.Create, FileAccess.Write, FileShare.None))
{
BinaryFormatter binFormat = new BinaryFormatter();
binFormat.Serialize(fStream, GAContents);
}
}
When running the above code I get the following exception:
System.Runtime.Serialization.SerializationException was unhandled
Message=Type 'WindowsFormsApplication1.Form1' in Assembly 'GeneticAlgApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
So that means that somewhere in the objects I'm trying to save there must be a reference to the main form. The only possible references I can see are 3 delegates which all point to methods in the main form code. Do delegates get serialized as well? I can't seem to apply the [NonSerialized] attribute to them.
Is there anything else I might be missing? Even better, is there a quick method to find the reference(s) that are causing the problem?