So here’s the problem ,you want to store items in a Dictionary *but* you want to be able to access those items using different types of key (so in the example below, I use Guids, strings and longs)…here’s how I usually do this…as usual any comments or suggestions for better ways are appreciated…

using System;

using System.Collections;

 

namespace Configuration

{

    /// <summary>

    /// Summary description for ContentPageDictionary.

    /// </summary>

    public class ContentPageDictionary : DictionaryBase

    {

        private Hashtable cpKeys = new Hashtable();

        private Hashtable cpLongKeys = new Hashtable();

        public new void Clear()

        {

            this.Dictionary.Clear();

            cpKeys.Clear();

            cpLongKeys.Clear();

        }

        public  ContentPage this[string key]

        {

            get

            {

                return (ContentPage)base.Dictionary[key];

            }

            set

            {

                Dictionary.Add(key, value);

                cpKeys.Add(value.Guid, key);

                cpLongKeys.Add(value.ObjectId, key);

            }

        }

 

        public ContentPage  this[long key]

            {

        get

            {

            if(cpLongKeys.Contains(key))

            return  this[cpLongKeys[key] as string];

            return null;

        }

    }

        public ContentPage this[Guid key]

        {

            get

            {

                if(cpKeys.Contains(key))

                    return  this[cpKeys[key] as string];

                return null;

            }

        }

            public ICollection Keys

        {

            get { return (Dictionary.Keys); }

        }

 

        public ICollection Values

        {

            get { return (Dictionary.Values); }

        }

 

        public void Add(string key, ContentPage value)

        {

            Dictionary.Add(key, value);

        }

 

        public bool Contains(string key)

        {

            return (Dictionary.Contains(key));

        }

 

        public void Remove(string key)

        {

            Dictionary.Remove(key);

        }

 

        protected override void OnInsert(Object key, Object value)

        {

            if (key.GetType() != typeof (string))

                throw new ArgumentException("key must be of type string.", "key");

 

            if (value.GetType() != typeof (ContentPage))

                throw new ArgumentException("value must be of type ContentPage.", "value");

        }

 

        protected override void OnRemove(Object key, Object value)

        {

            if (key.GetType() != typeof (string))

                throw new ArgumentException("key must be of type string.", "key");

        }

 

        protected override void OnSet(Object key, Object oldValue, Object newValue)

        {

            if (key.GetType() != typeof (string))

                throw new ArgumentException("key must be of type string.", "key");

 

            if (oldValue.GetType() != typeof (ContentPage))

                throw new ArgumentException("value must be of type ContentPage.", "oldValue");

            if (newValue.GetType() != typeof (ContentPage))

                throw new ArgumentException("value must be of type ContentPage.", "newValue");

        }

 

        protected override void OnValidate(Object key, Object value)

        {

            if (key.GetType() != typeof (string))

                throw new ArgumentException("key must be of type string.", "key");

 

            if (value.GetType() != typeof (ContentPage))

                throw new ArgumentException("value must be of type ContentPage.", "value");

        }

 

    }

}