Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Collection serialization problem...

Collection serialization problem...

Scheduled Pinned Locked Moved C#
jsonhelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shy Agam
    wrote on last edited by
    #1

    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 as Serializable 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 a BinaryFormatter, I get an exception saying that my main form is not marked as Serializable. Why on earth does it look for a Serializable attribute on the main form?? What am I missing here? :confused: Thanks in advance, Shy.

    D P S 3 Replies Last reply
    0
    • S Shy Agam

      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 as Serializable 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 a BinaryFormatter, I get an exception saying that my main form is not marked as Serializable. Why on earth does it look for a Serializable attribute on the main form?? What am I missing here? :confused: Thanks in advance, Shy.

      D Offline
      D Offline
      Dave Herren
      wrote on last edited by
      #2

      Please provide sample code.

      topcoderjax - Remember, Google is your friend.

      S 1 Reply Last reply
      0
      • D Dave Herren

        Please provide sample code.

        topcoderjax - Remember, Google is your friend.

        S Offline
        S Offline
        Shy Agam
        wrote on last edited by
        #3

        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(); } } } ... }

        1 Reply Last reply
        0
        • S Shy Agam

          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 as Serializable 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 a BinaryFormatter, I get an exception saying that my main form is not marked as Serializable. Why on earth does it look for a Serializable attribute on the main form?? What am I missing here? :confused: Thanks in advance, Shy.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • S Shy Agam

            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 as Serializable 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 a BinaryFormatter, I get an exception saying that my main form is not marked as Serializable. Why on earth does it look for a Serializable attribute on the main form?? What am I missing here? :confused: Thanks in advance, Shy.

            S Offline
            S Offline
            Shy Agam
            wrote on last edited by
            #5

            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 implement ISerializable, and prevent the events from getting serialized. :) Thanks for you guidence, Shy.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups