Simba White

Simba White

  • NA
  • 32
  • 7.8k

Can't access a Custom Serializable Class Object from project

Feb 7 2017 9:05 AM

I'm trying to use a Serializable Dictionary as a type in the project Settings.settings. I have found a solution for my issue.

http://stackoverflow.com/questions/1166496/key-value-storage-in-settings-file 
 

I have created the SerializableDictionary class and compiled my project.

Issue: I'm not able to select this class as a type in the project Settings.settings

I have included a reference to the assembly where I created the class. Some of the classes in that assembly appear when I try to browse but not this SerializableDictionary. I also tried to type the full name of the class in the Selected Type box got an error :  SerializableDictionary is not defined.

Can someone help please ?

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Xml.Serialization;  
  5.   
  6. namespace mynamespace  
  7. {  
  8.   
  9. [XmlRoot("dictionary")]  
  10. public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
  11.  
  12. {  
  13.     #region IXmlSerializable Members  
  14.     public System.Xml.Schema.XmlSchema GetSchema()  
  15.     {  
  16.         return null;  
  17.     }   
  18.     public void ReadXml(System.Xml.XmlReader reader)  
  19.     {  
  20.         XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));  
  21.         XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));  
  22.         bool wasEmpty = reader.IsEmptyElement;  
  23.         reader.Read();  
  24.   
  25.         if (wasEmpty)  
  26.             return;  
  27.         while (reader.NodeType != System.Xml.XmlNodeType.EndElement)  
  28.         {  
  29.             reader.ReadStartElement("item");   
  30.             reader.ReadStartElement("key");  
  31.             TKey key = (TKey)keySerializer.Deserialize(reader);  
  32.             reader.ReadEndElement();  
  33.             reader.ReadStartElement("value");  
  34.             TValue value = (TValue)valueSerializer.Deserialize(reader);  
  35.             reader.ReadEndElement();  
  36.             this.Add(key, value);  
  37.             reader.ReadEndElement();  
  38.             reader.MoveToContent();  
  39.         }  
  40.         reader.ReadEndElement();  
  41.     }  
  42.     public void WriteXml(System.Xml.XmlWriter writer)  
  43.     {  
  44.         XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));  
  45.         XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));  
  46.         foreach (TKey key in this.Keys)  
  47.         {  
  48.   
  49.             writer.WriteStartElement("item");  
  50.             writer.WriteStartElement("key");  
  51.             keySerializer.Serialize(writer, key);  
  52.             writer.WriteEndElement();  
  53.             writer.WriteStartElement("value");  
  54.             TValue value = this[key];  
  55.             valueSerializer.Serialize(writer, value);  
  56.             writer.WriteEndElement();  
  57.             writer.WriteEndElement();  
  58.         }  
  59.     }  
  60.     #endregion  
  61. }  
  62. }  
 
 

Answers (1)