473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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 SerializationInfo and StreamingContext 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-

Expand|Select|Wrap|Line Numbers
  1. [Serializable]
  2. public class MyClass : ISerializable
  3. {
  4. internal  static Dictionary<long, string> dict = new Dictionary<long,string>();
  5. public void GetObjectData(SerializationInfo info, StreamingContext context)
  6.  {
  7. info.AddValue("static.dic", MyClass1.dict, typeof(Dictionary<long, string>));
  8.  }
  9. public MyClass(SerializationInfo info, StreamingContext context)
  10.  {
  11.             MyClass.dict= (Dictionary<long, string>)info.GetValue("static.dic", typeof(Dictionary<long, string>));
  12. }
  13.  public void Add()
  14.         {
  15.             dict.Add(21, "11");
  16.         }
  17. public MyClass()
  18.   {
  19.            dict.Add(21, "11");
  20. }
  21. }
  22. public class MyClass 
  23.     {
  24.         static Dictionary<int, string> dict = new Dictionary<int,string>();
  25.         MyClass myClass = new MyClass();
  26.         public static void Main()
  27.         {
  28.  myClass.Add();
  29.  FileStream fileStream = new FileStream("test.binary", FileMode.Create);
  30.             IFormatter  bf = new BinaryFormatter();
  31.            bf.Serialize(fileStream, myClass);
  32.             fileStream.Dispose();
  33.             fileStream.Close();
  34.              fileStream = new FileStream("test.binary", FileMode.Open);
  35.         bf = new BinaryFormatter();
  36.             myClass = (MyClass1)bf.Deserialize(fileStream);
  37. }
  38. }
Aug 3 '09 #1
2 2796
Plater
7,872 Expert 4TB
Try adding an Application.DoEvents() 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.
Aug 3 '09 #2
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1.              fileStream = new FileStream("test.binary", FileMode.Open);
  2.         bf = new BinaryFormatter();
  3.             myClass = (MyClass1)bf.Deserialize(fileStream);
  4.  
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.
Aug 3 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jason | last post by:
I have a class that inherits from CollectionBase. My derived class raises events whenever items are added or removed from the collection. When I try to serialize my class I get errors because of...
7
by: Lars-Erik Aabech | last post by:
Hi! I've got problems with serializing my collections of business objects. The objects themselves serialize fine, but the collections fail. I've got the following structure: Base collection...
3
by: Nadav | last post by:
Hi, I have to read files generated by 3rd party application, these files are in a propriotary format ( not generated using the .NET framework ), I want to implement a custom DeSerializer to read...
2
by: Nadav | last post by:
Hi, Introduction: **************** I am trying to DeSerialize a file that was created by an unmanaged application by the binary formatter, to achieve that I am implementing the following...
2
by: MP | last post by:
Hello, I am having a bit of a problem with Deserialization. I need to "override" the special constructor used when an object is deserialize. In thes constructor I have to execute some code that...
3
by: Urs Vogel | last post by:
Hi Let's assume that I have an object_V1 (version 1) serialized to disk. Later, the class for object_V1 will change to object_V2, having some more members. Now, I would like to read my...
0
by: PeterW | last post by:
I have a class which uses three other classes, two of which are used in arrays. This class is only ever used for incoming messages. All classes inherit from ISerializable and impliment...
12
by: yoshijg | last post by:
Is there a way to be notified when the deserialization of an XML is complete, like a callback method? I tried the IDeserializationCallback interface, but that doesnt for XML-Deserialization. ...
1
by: RSH | last post by:
Hi, I have a situation where I am serializing a custom object into an XML string that is stored on a Session variable. The problem is I can't seem to figure out how to get it back to an...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.