Skip to content

Can't access a Custom Serializable Class Object from project

Simba White

Asked Feb 7, 2017, 4:05 AM EST1 answer
Share
LinkedInX

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

 

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. }  
 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • +1
    The settings does not support generic classes. I have made the class non generic.
    Now, I am able to pick this a class as a type :)
     
    However, when adding an item to the settings it throwing an exception.
      
    Properties.Settings.Default.userSetting.Add("Bird","Eagle");
    It throws a null reference exception. Even though I have defined userSetting in the Settings.settings.
     
      
    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 : Dictionary<string, string>, 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(string));    
    21.         XmlSerializer valueSerializer = new XmlSerializer(typeof(string));    
    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.             string key = (string)keySerializer.Deserialize(reader);    
    32.             reader.ReadEndElement();    
    33.             reader.ReadStartElement("value");    
    34.             string value = (string)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(string));    
    45.         XmlSerializer valueSerializer = new XmlSerializer(typeof(string));    
    46.         foreach (string 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.             string value = this[key];    
    55.             valueSerializer.Serialize(writer, value);    
    56.             writer.WriteEndElement();    
    57.             writer.WriteEndElement();    
    58.         }    
    59.     }    
    60.     #endregion    
    61. }    
    62. }    
     
    +1