c# - Serialize Class containing Dictionary member -


On my extension, I have decided that (D) serialize my config file class which works great

Now I want to store an associative array of drive letters (key is the drive letter, value is the network path) and dictionary , hybridDictionary , and < Code> hashtable for this but I always get the following error when calling ConfigFile.Load () or ConfigFile.Save () :

There was an error indicating the type 'App.ConfigFile' [Snip] System.NotSupportedException: Members can not serialize the app.configfile.mapdrives [snip]

I The dictionary is read and the hashtab can be serial, so what am I doing?

  [XmlRoot (ElementName = "Config")] Public class ConfigFile {public string GUIPath {get; Set; } Public String ConfigPath {get; Set; } Public Dictionary & lt; String, string & gt; Mappread = new dictionary & lt; String, string & gt; (); Public Boolean Save (String Filename) {{var filestream = File.Open (File Name, FileMode.OpenOrCreate, FileAccess.ReadWrite)) {try serializer = new XmlSerializer (typeF (ConfigFile)); Serializer.Serialize (filestream, this); Back true; } Hold (exception e) {message box. Show (E. Message); return false; }}} Public Zero addDrive (string drvLetter, string path) {this.mappedDrives.Add (drvLetter, path); } Public Static ConfigFile Load (String Filename) {{var filestream = File.Open (File Name, FileMode.Open, FileAccess. Read)} {try {var serializer = new XmlSerializer (typeF (ConfigFile)); Return (config file) serializer Diaryialize (filestream); } Hold (Exception pre) {Message Box. Show (ex.Message + ex.ToString ()); Return new config (); }}}}}  

You can serialize a class that implements IDictionary is. See this.

Q: Why can not I serial the hashtables?

A: XmlSerializer can not process the IDictionary interface classes. It was partly due to the constraints of the constraints and partly due to the fact that a hashtable does not have equivalent in the XSD type system. The only solution is to implement a custom hashtable that does not implement the IDictionary interface.

So I think you need to make your own version of the dictionary for this. Check it out.


Comments