Deserializing a Dictionary Object

January 15, 2008 at 12:33 am 3 comments

As part of an application I am maintaining user options in a Dictionary object that I can serialize to a user directory to survive the install of updated version of the appliation via Click Once.  Serializing was fine, when I tried to deserialize I received an error.

Thanks to a post by Miha Markic (http://cs.rthand.com/blogs/blog_with_righthand/archive/2005/08/06/92.aspx) I have the problem sorted.  The code is:

[Serializable]
public class Options : Dictionary<string, Object>
{
public static string DefaultFileName
{
get { return Util.DataDirectory + “options.bin”; }

}
private static Options tempOptions;

public Options()
: base()
{
}
public Options(SerializationInfo si, StreamingContext sc)
: base(si, sc)
{
}

public static Options Load()
{
return Load(DefaultFileName);
}
public static Options Load(string fileName)
{
Options tempOptions;
FileStream fs = new FileStream(fileName, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
tempOptions = (Options)bf.Deserialize(fs);
fs.Close();
return tempOptions;
}

Advertisements

Entry filed under: Uncategorized.

Medion Design USB Graphics Pad P46000

3 Comments Add your own

  • 1. suresh  |  July 16, 2008 at 6:53 am

    i have a problem in deserializing xml back because i have idictionary, i have a baseclass which derives more than one class. the base class has its own properties plus the idictionary. Im using ixmlserializer to serialize it. And it happens too, stucked up in deserializing it back to my objects. Any helps

    Reply
  • 2. ilgeniodellafuga  |  September 10, 2009 at 9:03 am

    I Have the same problem! I can’t find solution! HELP

    Reply
  • 3. Deserialize Dictionary @merlinox  |  December 29, 2014 at 9:37 pm

    […] subito Miha Markič e Brent Challis per la preziosa collaborazione, anche senza che io gli avessi detto niente. Devono essere dei veri […]

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

January 2008
M T W T F S S
    Jul »
 123456
78910111213
14151617181920
21222324252627
28293031  

Most Recent Posts


%d bloggers like this: