Rating: 1 Star2 Stars3 Stars4 Stars5 Stars
Loading...

Serialize hash table in C# dynamically

I have realized while working on a problem that .NET Framework does not allow serialization of any object that implements the IDictionary interface. I was very much disappointing with the restriction but later i figured out that the restriction is not on hash tables. There are few ways which leads people to follow the following steps in order to  solve this problem

  • Few developers prefer to find an alternative to hash tables
  • Few developers implement the IXmlSerializable interface in a custom object and wrap it around the hash table.
  • My approach is to use a generic List<T> object as a base and implement the standard features used in a hash table within that object. The List<T> derivative can then be serialized.

Using List<T> as a base

List<T> objects are serializable, and this serialization is automatically handled in the .NET Framework. This means we will not have to implement our own serialization routines, and it’s one of the reasons I decided to use List<T> as a base for my solution.

Using List<T> also provides you with some “free” functionality as a result of inheritance:

  • Automatically keeps track of the number of entries
  • Gives us the ability to use the Find methods (FindAll, FindLast, Find, etc….)
  • Gives us the ability to use the Exists method
  • Gives us the ability to use Foreach() looping
  • Gives us the ability to use Sorting (and the ability to guarantee the sorting of objects within the list)

So obviously, the List<T> object gives us a nice start. However, there are still a few methods we need to implement within our object so that we have the basic hash table functionality:

  • An Add(key, value) method, which allows you to add values to the table
  • The ability to pull a value via a key (like the hash table’s indexer)
  • The ability to remove a value via a key
  • The ability to see if a key already exists or not

There are also a few other features that will make using this object easier:

  • A constructor that accepts a hash table and enumerates through the hash table to populate the object
  • A property that returns a hash table

With that list of features, we now have an object that supports the most commonly used hash table functions, can be translated automatically to/from a hash table, and is also serialisable. I chose to call this object the OrderedTable.

The OrderedTable object’s code

The first step in coding an OrderedTable is to inherit from the List<T> object and mark the object as serialisable (See the code below).

You’ll notice that this inherits from NameValuePair, which is a simple object with two properties — Name and Value. This object allows us to store/access the List’s data in a similar way that a hash table does.

The next step will be to create a constructor that accepts a hash table and converts it into an OrderedTable. The code for this constructor is shown below.

This function uses the GetEnumerator method on the hash table object to get a reference to an IDictionaryEnumerator interface. The IDictionaryEnumerator interface is then used to loop through the hash table’s data. Each key/value pair in the hash table is then added to the OrderedTable.

At this point, we need to create an Add() method that can add data into our OrderedList. The code for this is shown below.

This method simply accepts a key/value and then creates a NameValuePair object to add to the OrderedTable. You’ll notice that this method uses base.Add — the reason for this is that we want to add the NameValuePair object to the base List<T> object.

The indexer on the OrderedTable accepts a key and returns the value associated with that key. This is similar to how the hash table’s indexer works. The code for this is shown below.

This property uses the Find() functions as mentioned above to execute a delegate against the objects (NameValuePairs) stored within the OrderedTable. After the correct object is found, we either set the value or return the value depending on which accessor we’re in.

The last important property in the OrderedTable class is the HashTable property. This property takes care of the work of converting the OrderedTable back into a hash table. The code is shown below.

This property loops through the current OrderedTable and adds objects to a hash table for each object in the OrderedTable. It then returns the fully populated hash table.

Serialisation example

The code below is an example of first creating a hash table, converting it to an OrderedTable, serialising the OrderedTable, and deserialising the OrderedTable to give us access again to the hash table.

The code below shows what the OrderedTable.xml looks like after the OrderedTable is serialised.

Leave me a comment and let me hear your opinion. If you’ve got any thoughts, comments or suggestions for things we could add, leave a comment! Also please Subscribe to our RSS for latest tips, tricks and examples on cutting edge stuff.

Related Posts

logo_sql

Top 12 Smart Tips To Work With SQL

activex-data-objects

Best Practices of ActiveX Data Object (ADO)

FileSystemWatcher to monitor directory changes in C#

11 reasons to use Access and few not to

  • Daniel

    your code is incomplete. next time link to the source if you're not going to include all code snippets.

  • Daniel i don't think its incomplete. Please let me know where you are having problem and what part according to you isn't complete. The above code gives you an idea on how you can serialize your hash tables in C#. Please follow the steps given above and of course you have to make modifications to it as per you need.

  • Daniel

    your code is incomplete. next time link to the source if you're not going to include all code snippets.

  • Daniel i don't think its incomplete. Please let me know where you are having problem and what part according to you isn't complete. The above code gives you an idea on how you can serialize your hash tables in C#. Please follow the steps given above and of course you have to make modifications to it as per you need.