DeSerialization doesn’t work though i Implement GetObjectData method and Constructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • punitsinghi
    New Member
    • Aug 2009
    • 1

    DeSerialization doesn’t work though i Implement GetObjectData method and Constructor

    Hi,

    I have a static generic dictionary in a class. As static memeber cannot serialized so i have implented ISerializable interface and method GetObjectData to serialize. I have a constructor which will also accept SerializationIn fo and StreamingContex t to deserliaze the dictionay. Now when i try to serialize and deserialize , it always return 1(thoug i added 2 entries). please find the pseduo code-

    Code:
    [Serializable]
    public class MyClass : ISerializable
    {
    internal  static Dictionary<long, string> dict = new Dictionary<long,string>();
    public void GetObjectData(SerializationInfo info, StreamingContext context)
     {
    info.AddValue("static.dic", MyClass1.dict, typeof(Dictionary<long, string>));
     }
    public MyClass(SerializationInfo info, StreamingContext context)
     {
                MyClass.dict= (Dictionary<long, string>)info.GetValue("static.dic", typeof(Dictionary<long, string>));
    }
     public void Add()
            {
                dict.Add(21, "11");
            }
    public MyClass()
      {
               dict.Add(21, "11");
    }
    }
    public class MyClass 
        {
            static Dictionary<int, string> dict = new Dictionary<int,string>();
            MyClass myClass = new MyClass();
            public static void Main()
            {
     myClass.Add();
     FileStream fileStream = new FileStream("test.binary", FileMode.Create);
                IFormatter  bf = new BinaryFormatter();
               bf.Serialize(fileStream, myClass);
                fileStream.Dispose();
                fileStream.Close();
                 fileStream = new FileStream("test.binary", FileMode.Open);
            bf = new BinaryFormatter();
                myClass = (MyClass1)bf.Deserialize(fileStream);
    }
    }
    Last edited by tlhintoq; Aug 3 '09, 05:41 PM. Reason: [CODE] ...your code goes here... [/CODE] tags added
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Try adding an Application.DoE vents() and maybe a Thread.Sleep() after you call the bf.Serialize() call. Or, if it supposts it, a Flush() call.
    It is sometimes the case with IO calls and streams, that the function returns before it is actually "done" and you then you immediatly dispose and close of the object, meaning it never gets to finish.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Code:
                   fileStream = new FileStream("test.binary", FileMode.Open);
              bf = new BinaryFormatter();
                  myClass = (MyClass1)bf.Deserialize(fileStream);
      I realize this is just to test whether or not the save worked but take it out of the save routine. It doesn't really belong there.

      If you want to test the saving then call the save routine... let it complete and exit... then call the load routine to check it.

      Comment

      Working...