using System;
using System.Collections;
using System.Web.UI;
namespace umbraco
{
///
/// Class that adapts an to the interface.
///
public class AttributeCollectionAdapter : IDictionary
{
private readonly AttributeCollection _collection;
///
/// Initializes a new instance of the class.
///
/// The collection.
public AttributeCollectionAdapter(AttributeCollection collection)
{
_collection = collection;
}
#region IDictionary Members
///
/// Adds an element with the provided key and value to the object.
///
/// The to use as the key of the element to add.
/// The to use as the value of the element to add.
public void Add(object key, object value)
{
_collection.Add(key.ToString(), value.ToString());
}
///
/// Removes all elements from the object.
///
///
/// The object is read-only.
///
public void Clear()
{
_collection.Clear();
}
///
/// Determines whether the object contains an element with the specified key.
///
/// The key to locate in the object.
///
/// true if the contains an element with the key; otherwise, false.
///
public bool Contains(object key)
{
return _collection[key.ToString()] != null;
}
///
/// Returns an object for the object.
///
///
/// An object for the object.
///
public IDictionaryEnumerator GetEnumerator()
{
return new AttributeCollectionAdapterEnumerator(this);
}
///
/// Gets a value indicating whether the object has a fixed size.
///
///
/// true if the object has a fixed size; otherwise, false.
///
public bool IsFixedSize
{
get { return false; }
}
///
/// Gets a value indicating whether the object is read-only.
///
///
/// true if the object is read-only; otherwise, false.
///
public bool IsReadOnly
{
get { return false; }
}
///
/// Gets an object containing the keys of the object.
///
///
///
/// An object containing the keys of the object.
///
public ICollection Keys
{
get { return _collection.Keys; }
}
///
/// Removes the element with the specified key from the object.
///
/// The key of the element to remove.
public void Remove(object key)
{
_collection.Remove(key.ToString());
}
///
/// Gets an object containing the values in the object.
///
///
///
/// An object containing the values in the object.
///
public ICollection Values
{
get { throw new NotImplementedException(); }
}
///
/// Gets or sets the with the specified key.
///
///
public object this[object key]
{
get { return _collection[key.ToString()]; }
set { _collection[key.ToString()] = value.ToString(); }
}
#endregion
#region ICollection Members
/// Not implemented.
/// The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing.
/// The zero-based index in at which copying begins.
public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}
///
/// Gets the number of elements contained in the .
///
///
///
/// The number of elements contained in the .
///
public int Count
{
get { return _collection.Count; }
}
///
/// Gets a value indicating whether access to the is synchronized (thread safe).
///
///
/// true if access to the is synchronized (thread safe); otherwise, false.
///
public bool IsSynchronized
{
get { return false; }
}
///
/// Gets an object that can be used to synchronize access to the .
///
///
///
/// An object that can be used to synchronize access to the .
///
public object SyncRoot
{
get { return _collection; }
}
#endregion
#region IEnumerable Members
///
/// Returns an enumerator that iterates through a collection.
///
///
/// An object that can be used to iterate through the collection.
///
IEnumerator IEnumerable.GetEnumerator()
{
foreach (object key in _collection.Keys)
yield return _collection[(string)key];
}
#endregion
///
/// for the class.
///
private class AttributeCollectionAdapterEnumerator : IDictionaryEnumerator
{
private readonly AttributeCollectionAdapter _adapter;
private readonly IEnumerator _enumerator;
///
/// Initializes a new instance of the class.
///
/// The adapter.
public AttributeCollectionAdapterEnumerator(AttributeCollectionAdapter adapter)
{
_adapter = adapter;
_enumerator = ((IEnumerable)adapter).GetEnumerator();
}
#region IDictionaryEnumerator Members
///
/// Gets both the key and the value of the current dictionary entry.
///
///
///
/// A containing both the key and the value of the current dictionary entry.
///
///
/// The is positioned before the first entry of the dictionary or after the last entry.
///
public DictionaryEntry Entry
{
get { return new DictionaryEntry(Key, Value); }
}
///
/// Gets the key of the current dictionary entry.
///
///
///
/// The key of the current element of the enumeration.
///
///
/// The is positioned before the first entry of the dictionary or after the last entry.
///
public object Key
{
get { return _enumerator.Current; }
}
///
/// Gets the value of the current dictionary entry.
///
///
///
/// The value of the current element of the enumeration.
///
///
/// The is positioned before the first entry of the dictionary or after the last entry.
///
public object Value
{
get { return _adapter[_enumerator.Current]; }
}
#endregion
#region IEnumerator Members
///
/// Gets the current element in the collection.
///
///
///
/// The current element in the collection.
///
///
/// The enumerator is positioned before the first element of the collection or after the last element.
///
public object Current
{
get { return _enumerator.Current; }
}
///
/// Advances the enumerator to the next element of the collection.
///
///
/// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
///
///
/// The collection was modified after the enumerator was created.
///
public bool MoveNext()
{
return _enumerator.MoveNext();
}
///
/// Sets the enumerator to its initial position, which is before the first element in the collection.
///
///
/// The collection was modified after the enumerator was created.
///
public void Reset()
{
_enumerator.Reset();
}
#endregion
}
}
}