Collection serialization problem...
-
Hello, I have a collection class I wrote. It is marked as
Serializable
. Every class which instances are to be stored in the collection are marked asSerializable
as well. I create an instance of the collection in my main form, and handle it from there. When I try to serialize the collection using aBinaryFormatter
, I get an exception saying that my main form is not marked asSerializable
. Why on earth does it look for aSerializable
attribute on the main form?? What am I missing here? :confused: Thanks in advance, Shy. -
Hello, I have a collection class I wrote. It is marked as
Serializable
. Every class which instances are to be stored in the collection are marked asSerializable
as well. I create an instance of the collection in my main form, and handle it from there. When I try to serialize the collection using aBinaryFormatter
, I get an exception saying that my main form is not marked asSerializable
. Why on earth does it look for aSerializable
attribute on the main form?? What am I missing here? :confused: Thanks in advance, Shy.Please provide sample code.
topcoderjax - Remember, Google is your friend.
-
Please provide sample code.
topcoderjax - Remember, Google is your friend.
Here is the main concept of how stuff get done: [Serializable()] public class TaskCollection : KeyedCollection<......> { ...... } public partial class MainForm : Form { ... private TaskCollection tasks; ... public void DoSave() { string fileName; // Do stuff and figure out fileName TaskSerializer.Serialize(tasks, fileName); } ... } public static class TaskSerializer { ... public static bool Serialize(TaskCollection tasks, string file, out string errorMsg) { FileStream stream = null; // Validation and preperations for serialization try { // Validation and preperations for serialization IFormatter formatter = new BinaryFormatter(); stream = new FileStream(file, FileMode.Create, FileAccess.Write); formatter.Serialize(stream, tasks); errorMsg = string.Empty; return true; } catch (Exception ex) { errorMsg = ex.Message; return false; } finally { if (stream != null) { stream.Close(); stream.Dispose(); } } } ... }
-
Hello, I have a collection class I wrote. It is marked as
Serializable
. Every class which instances are to be stored in the collection are marked asSerializable
as well. I create an instance of the collection in my main form, and handle it from there. When I try to serialize the collection using aBinaryFormatter
, I get an exception saying that my main form is not marked asSerializable
. Why on earth does it look for aSerializable
attribute on the main form?? What am I missing here? :confused: Thanks in advance, Shy.Somewhere in the collection chain, there is a reference to the form. This should be marked as NonSerialized. Note that serialization works throughout all of the referenced member types, so you may find that it's in a class that you didn't even think was being serialized.
Deja View - the feeling that you've seen this post before.
-
Hello, I have a collection class I wrote. It is marked as
Serializable
. Every class which instances are to be stored in the collection are marked asSerializable
as well. I create an instance of the collection in my main form, and handle it from there. When I try to serialize the collection using aBinaryFormatter
, I get an exception saying that my main form is not marked asSerializable
. Why on earth does it look for aSerializable
attribute on the main form?? What am I missing here? :confused: Thanks in advance, Shy.Problem solved... It appears that events are also serialized, which makes the
Serialize()
method go through the entire invocation lists inside of the events, and serialize them as well. This ends up in the objects registered to handle the events (the main form in my case) also getting serialized. So the solution I chose was to implementISerializable
, and prevent the events from getting serialized. :) Thanks for you guidence, Shy.