CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2006
    Posts
    130

    Serialization, why does this work?

    I have an object, we can call it ClassA, that are to be serialized. It's containing an arraylist. It looks something like this

    Code:
    [Serializable()]
    public class ClassA {
          private ArrayList list;
          public ArrayList List {
                    // get/set
          }
    
          public override void GetObjectData(SerializationInfo info, StreamingContext ctxt)
            {
                  info.AddValue("list", list);
            }
    }
    and also a constructor for the deserialization. The thing is that I store objects of type ClassB in my ArrayList. I've marked this class (ClassB) as Serializable but it does not implement ISerializable or has a GetObjectData method. Now the question is: Why is this working? How can the ArrayList populated with ClassB-objects be serialized when there is no serialize-method for ClassB? Because everything works fine and when I deserialize my object, the arraylist contains the same stuff as it did before the serialization.

  2. #2
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Serialization, why does this work?

    When you mark a class with the Serializable attribute you don't need to implement the interace ISerializable. The default behavior will occur when there is a need to serialize or deserialize the object.
    If you need a special behavior then you should implement the interface.

  3. #3
    Join Date
    Jun 2006
    Posts
    130

    Re: Serialization, why does this work?

    Which is the default behavior? Because I have other classes, and I need to implement ISerializable in them or otherwise I will get the wrong values (e.g. if a value is true, after serialization/deserialization, it will be false and so on)..but it works on some classes without..
    Last edited by EEKstream; July 27th, 2006 at 03:17 AM.

  4. #4
    Join Date
    Dec 2005
    Posts
    282

    Re: Serialization, why does this work?

    you need to implement this only if you want to serialize data, which is not pubilc or if you have to save some data which isn't serializable.

    normally on bool value this should work without problems. maybe you have some other errors or problems in your code...which serializer do you use?

    Regards
    Hansjörg

  5. #5
    Join Date
    Jun 2006
    Posts
    130

    Re: Serialization, why does this work?

    I found it, it had nothing to do with the serialization. Thanks, this saved me a lot of code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured