0

I need to construct a serializable, observable dictionary. It must be observable so that I know when it changes, and it must be serializable because I need to be able to save it via Visual Studios User Settings.

I have found a considerable amount of help in this regard, but I am having some difficulty with the constraints for the generics.

For right now, the dictionary will be of have as its Key Type <System.Windows.Input.Key>, and as the value <System.IO.FileInfo>.

This is what I have so far : Again, I need help with the constraints on the generic types :

using System;
using System.Linq;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Xml.Serialization;
using System.Xml;
using System.Runtime.Serialization;

namespace System.Collections.ObjectModel {
    public class ObservableDictionary<TKey, TValue> :
        IDictionary<TKey, TValue>, INotifyCollectionChanged, INotifyPropertyChanged, IXmlSerializable
        where TKey : ISerializable
        where TValue : class, ISerializable, new() {
        private const string CountString = "Count";
        private const string IndexerName = "Item[]";
        private const string KeysName = "Keys";
        private const string ValuesName = "Values";

        /*Observable Stuff*/

        public Xml.Schema.XmlSchema GetSchema( ) { return null; }

        public void ReadXml( Xml.XmlReader reader ) {
            while ( reader.Read( ) && !(reader.NodeType == XmlNodeType.EndElement && reader.LocalName == this.GetType().Name ) ) {
                TKey Key = reader["Key"] as TKey;
                if ( Key == null )
                    throw new FormatException( );
                this[Key] = reader["Value"] as TValue;
            }
        }

        public void WriteXml( Xml.XmlWriter writer ) {
            this.Keys.ToList( ).ForEach( K => {
                writer.WriteStartElement( "KeyValuePair" );
                writer.WriteAttributeString( "Key", K.ToString( ) );
                writer.WriteAttributeString( "Value", this[K].ToString( ) );
                writer.WriteEndElement( );
            } );
        }
    }
}

I'm pretty certain that I will have to implement serialization on the FileInfo class, but for now I just want to focus on having a dictionary that can take generics that can be serialized. I was hoping to go with XML but if I need to do it Binary, I can live with that...

So, what Constraints do I need to put on the Generics TKey and TValue such that I can be safe serializing the dictionary?

Community
  • 1
  • 1
Will
  • 3,413
  • 7
  • 50
  • 107
  • It seems dangerous to mix serialization and observableness, particularly when their are collections that respectively handle each. Perhaps you should consider using the already implemented solutions for each respective task. – David L May 07 '15 at 15:23
  • The reason for this is because I need to be able to save the dictionary into the User Settings (Serialization), and watch for changes to it so I can tell when I need to save my settings (Observable). Also; Why do you say it seems dangerous? – Will May 07 '15 at 15:31
  • If you're using WPF, you can actually serialize and save on app shut down. You don't need to write your changes every time they occur. In addition, you're limiting yourself to JUST xml serialization because your class implements it. If you instead split the functionality apart you could use any serialization methodology – David L May 07 '15 at 16:06
  • @DavidL Well, the issue is that I can't just save it when the program closes : There's a settings form that dictates if settings should be saved, reverted or reset. As for what you said, I am okay with any sort of serialization, I just need to see that it happens. How would I go about splitting the functionality? – Will May 07 '15 at 16:14
  • Use an ObservableCollection and, on save, call an external serializer and pass your data from the ObservableCollection to the serializer class. – David L May 07 '15 at 16:21
  • That defeats the purpose of having everything within the User Settings. – Will May 07 '15 at 17:42

0 Answers0