Link to home
Create AccountLog in
Avatar of seinc
seinc

asked on

How to serialize a custom collection class

I am trying to serialize a custom collection that I wrote (Class is shown below). It serializes fine, but I get nothing back out when I deserialize.
Can someone show me what I need to do to get my collection of objects back?

//my collection
[Serializable()]
      public class PlayerCollection:CollectionBase, ISerializable
      {

            public PlayerCollection()
            {
            }

            public PlayerCollection(SerializationInfo info, StreamingContext context)
            {

            }

            public virtual void Add(CrowCoder.Player NewPlayer)
            {
                  this.List.Add(NewPlayer);
            }

            public virtual Player this[int Index]
            {
       
                  get
                  {
                        //return the Widget at IList[Index]
                        return (Player)this.List[Index];
           
                  }
            }
            #region ISerializable Members

            public void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                  info.AddValue("Players", this.List);
            }
            #endregion

            public void SerializeMe()
            {
                  try
                  {
                        Stream stream = File.Open("PlayerList.plr", FileMode.OpenOrCreate);
                        BinaryFormatter bformatter = new BinaryFormatter();

                        bformatter.Serialize(stream, this);
                        stream.Close();
                  }
                  catch(Exception ex)
                  {

                  }
            }
      }

//Code to use the collection
            private void button1_Click(object sender, System.EventArgs e)
            {
                  CrowCoder.Player p1 = new Player("Joe", "Smith");
                  CrowCoder.Player p2 = new Player("Mary", "Smith");
                  CrowCoder.Player p3 = new Player("John", "Smith");
                  CrowCoder.Player p4 = new Player("Jane", "Smith");

                  CrowCoder.PlayerCollection pc = new PlayerCollection();

                  pc.Add(p1);
                  pc.Add(p2);
                  pc.Add(p3);
                  pc.Add(p4);

                  pc.SerializeMe();
            }

//Deserializing here, but collection comes up empty...........
            private void button2_Click(object sender, System.EventArgs e)
            {
                  CrowCoder.PlayerCollection mypc = new PlayerCollection();
                  Stream myStream;
                  BinaryFormatter bformatter = new BinaryFormatter();

                  myStream = File.Open("PlayerList.plr", FileMode.Open);
                  bformatter = new BinaryFormatter();

                  mypc = (CrowCoder.PlayerCollection)bformatter.Deserialize(myStream);
                  myStream.Close();

            }
Avatar of Ready1
Ready1
Flag of Australia image

just wanted to criticise your code, always use finally block when working with streams.

          public void SerializeMe()
          {
               Stream stream = null;
               try
               {
                    stream = File.Open("PlayerList.plr", FileMode.OpenOrCreate);
                    BinaryFormatter bformatter = new BinaryFormatter();

                    bformatter.Serialize(stream, this);
               }
               catch(Exception ex)
               {

               }
              finally
               {
                    stream.Close();
               }
          }

Does your "PlayerList.plr" have data in it?
Do you know that no exception was thrown during serialization?

REgards,
Ready1
Avatar of seinc
seinc

ASKER

This is what my PlayerList.plr has in it:
    ÿÿÿÿ          HMemoryGame, Version=1.0.2156.33581, Culture=neutral, PublicKeyToken=null   CrowCoder.PlayerCollection   PlayersCrowCoder.PlayerCollection              

It doesn't look like there is any data in it, but I wasn't sure since I'm looking at binary data  in Notepad.
it doesnt look like your CrowCoder.Player is serializaable only your PlayerCollection.

Regards,
Ready1
Avatar of seinc

ASKER

I can serialize and deserialize the Player class.
I think I'm missing something here:
          public PlayerCollection(SerializationInfo info, StreamingContext context)
          {

          }

but  I don't know what. I tried setting the collection's List but it is read only.
SOLUTION
Avatar of Ready1
Ready1
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Is the Player class serializable as well?
Avatar of seinc

ASKER

Yes, the player class is serializable and I have tested to make sure I can serialize and deserialize it.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of seinc

ASKER

Thanks dstanley9, I haven't had time to test this yet, but it looks like what I was missing. I'll get back soon.
Avatar of seinc

ASKER

Well, that doesn't work either. I made it a little furthur with the code below, using the collection's inner list:

            public PlayerCollection(SerializationInfo info, StreamingContext context)
            {
                  ArrayList myList = (ArrayList)info.GetValue("inner", typeof(ArrayList));

                  System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
                  
                  while(myEnumerator.MoveNext())
                  {
                              this.InnerList.Add(myEnumerator.Current);
                  }
            }

I'm giving up for now. I will just store my player objects separately. I found a reference to DesignerSerializationVisibilityAttribute but I can't apply it to my code.