Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1454
  • Last Modified:

Serialization of System.Collections.Specialized.StringDictionary

I need a custom subclass of the StringDictionary, which supports serialization.

and I need a helper class to serialize to a System.String, and not to a file.
 
0
bvrudney
Asked:
bvrudney
1 Solution
 
fahimnxbCommented:
0
 
bvrudneyAuthor Commented:
I have tried several different things, and can't get anything to work.  I've tried inheriting from StringDictionary, and finally, I've reflected the StringDictionary code, and just ported it right into my class.  I keep getting obscure errors from the xmlserializer:

"You must implement the Add(System.Object) method on PropertyDictionary because it inherits from IEnumerable."

If I remove the IEnumerable interface, I get this error:

You must implement a default accessor on System.Collections.ICollection because it inherits from ICollection.

which doesn't make any since, since it has an indexer.

Here is the code I'm using to serialize, along with my custom PropertyDictionary class:

private string SerializeCollection(PropertyDictionary pd)
{
      StringBuilder sb = new StringBuilder();
      XmlSerializer serializer = new XmlSerializer( typeof( PropertyDictionary ));
      TextWriter sw = new StringWriter( sb );

      serializer.Serialize( sw, pd );

      sw.Close();
      return sb.ToString();
}


using System;
using System.Runtime.Serialization;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Reflection;

[Serializable]
public class PropertyDictionary: IEnumerable, ISerializable,IDeserializationCallback
{
      SerializationInfo _savedSI;
      private Hashtable contents;

      public PropertyDictionary()
      {
            this.contents = new Hashtable();
      }

      public PropertyDictionary(SerializationInfo info, StreamingContext ctxt)
      {
            _savedSI = info;
      }
      void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
      {

            String[] keys  = new String[contents.Count];
            String[] values = new String[contents.Count];

            info.AddValue("Keys",   keys);
            info.AddValue("Values", values);
      }
      void IDeserializationCallback.OnDeserialization(Object sender)
      {

            String[] keys   = (String[])_savedSI.GetValue("Keys", typeof(String[]));
            String[] values = (String[])_savedSI.GetValue("Values", typeof(String[]));
            for (Int32 x = 0; x < keys.Length; x++)
            {
                  this.Add(keys[x], values[x]);
            }
            _savedSI = null;
      }
      public virtual void Add(string key, string value)
      {
            this.contents.Add(key.ToLower(CultureInfo.InvariantCulture), value);
      }
      public virtual void Clear()
      {
            this.contents.Clear();
      }
      public virtual bool ContainsKey(string key)
      {
            return this.contents.ContainsKey(key.ToLower(CultureInfo.InvariantCulture));
      }
      public virtual bool ContainsValue(string value)
      {
            return this.contents.ContainsValue(value);
      }
      public virtual void CopyTo(Array array, int index)
      {
            this.contents.CopyTo(array, index);
      }
      public virtual IEnumerator GetEnumerator()
      {
            return this.contents.GetEnumerator();
      }
      public virtual void Remove(string key)
      {
            this.contents.Remove(key.ToLower(CultureInfo.InvariantCulture));
      }
      public virtual int Count
      {
            get
            {
                  return this.contents.Count;
            }
      }
      public virtual bool IsSynchronized
      {
            get
            {
                  return this.contents.IsSynchronized;
            }
      }
      public virtual string this[string key]
      {
            get
            {
                  return (string) this.contents[key.ToLower(CultureInfo.InvariantCulture)];
            }
            set
            {
                  this.contents[key.ToLower(CultureInfo.InvariantCulture)] = value;
            }
      }
      public virtual ICollection Keys
      {
            get
            {
                  return this.contents.Keys;
            }
      }
      public virtual object SyncRoot
      {
            get
            {
                  return this.contents.SyncRoot;
            }
      }
      public virtual ICollection Values
      {
            get
            {
                  return this.contents.Values;
            }
      }
}
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now