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- - [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);
-
}
-
}
2 2796
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.
- 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |