Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

322 lines (293 sloc) 12.819 kB
//
// Copyright (c) 2004-2011 Jaroslaw Kowalski <jaak@jkowalski.net>
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Jaroslaw Kowalski nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
namespace NLog.Internal
{
using System;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Provides untyped IDictionary interface on top of generic IDictionary.
/// </summary>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
internal class DictionaryAdapter<TKey, TValue> : IDictionary
{
private readonly IDictionary<TKey, TValue> implementation;
/// <summary>
/// Initializes a new instance of the DictionaryAdapter class.
/// </summary>
/// <param name="implementation">The implementation.</param>
public DictionaryAdapter(IDictionary<TKey, TValue> implementation)
{
this.implementation = implementation;
}
/// <summary>
/// Gets an <see cref="T:System.Collections.ICollection"/> object containing the values in the <see cref="T:System.Collections.IDictionary"/> object.
/// </summary>
/// <value></value>
/// <returns>
/// An <see cref="T:System.Collections.ICollection"/> object containing the values in the <see cref="T:System.Collections.IDictionary"/> object.
/// </returns>
public ICollection Values
{
get { return new List<TValue>(this.implementation.Values); }
}
/// <summary>
/// Gets the number of elements contained in the <see cref="T:System.Collections.ICollection"/>.
/// </summary>
/// <value></value>
/// <returns>
/// The number of elements contained in the <see cref="T:System.Collections.ICollection"/>.
/// </returns>
public int Count
{
get { return this.implementation.Count; }
}
/// <summary>
/// Gets a value indicating whether access to the <see cref="T:System.Collections.ICollection"/> is synchronized (thread safe).
/// </summary>
/// <value></value>
/// <returns>true if access to the <see cref="T:System.Collections.ICollection"/> is synchronized (thread safe); otherwise, false.
/// </returns>
public bool IsSynchronized
{
get { return false; }
}
/// <summary>
/// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"/>.
/// </summary>
/// <value></value>
/// <returns>
/// An object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"/>.
/// </returns>
public object SyncRoot
{
get { return this.implementation; }
}
/// <summary>
/// Gets a value indicating whether the <see cref="T:System.Collections.IDictionary"/> object has a fixed size.
/// </summary>
/// <value></value>
/// <returns>true if the <see cref="T:System.Collections.IDictionary"/> object has a fixed size; otherwise, false.
/// </returns>
public bool IsFixedSize
{
get { return false; }
}
/// <summary>
/// Gets a value indicating whether the <see cref="T:System.Collections.IDictionary"/> object is read-only.
/// </summary>
/// <value></value>
/// <returns>true if the <see cref="T:System.Collections.IDictionary"/> object is read-only; otherwise, false.
/// </returns>
public bool IsReadOnly
{
get { return this.implementation.IsReadOnly; }
}
/// <summary>
/// Gets an <see cref="T:System.Collections.ICollection"/> object containing the keys of the <see cref="T:System.Collections.IDictionary"/> object.
/// </summary>
/// <value></value>
/// <returns>
/// An <see cref="T:System.Collections.ICollection"/> object containing the keys of the <see cref="T:System.Collections.IDictionary"/> object.
/// </returns>
public ICollection Keys
{
get { return new List<TKey>(this.implementation.Keys); }
}
/// <summary>
/// Gets or sets the <see cref="System.Object"/> with the specified key.
/// </summary>
/// <param name="key">Dictionary key.</param>
/// <returns>Value corresponding to key or null if not found</returns>
public object this[object key]
{
get
{
TValue value;
if (this.implementation.TryGetValue((TKey)key, out value))
{
return value;
}
else
{
return null;
}
}
set
{
this.implementation[(TKey)key] = (TValue)value;
}
}
/// <summary>
/// Adds an element with the provided key and value to the <see cref="T:System.Collections.IDictionary"/> object.
/// </summary>
/// <param name="key">The <see cref="T:System.Object"/> to use as the key of the element to add.</param>
/// <param name="value">The <see cref="T:System.Object"/> to use as the value of the element to add.</param>
public void Add(object key, object value)
{
this.implementation.Add((TKey)key, (TValue)value);
}
/// <summary>
/// Removes all elements from the <see cref="T:System.Collections.IDictionary"/> object.
/// </summary>
public void Clear()
{
this.implementation.Clear();
}
/// <summary>
/// Determines whether the <see cref="T:System.Collections.IDictionary"/> object contains an element with the specified key.
/// </summary>
/// <param name="key">The key to locate in the <see cref="T:System.Collections.IDictionary"/> object.</param>
/// <returns>
/// True if the <see cref="T:System.Collections.IDictionary"/> contains an element with the key; otherwise, false.
/// </returns>
public bool Contains(object key)
{
return this.implementation.ContainsKey((TKey)key);
}
/// <summary>
/// Returns an <see cref="T:System.Collections.IDictionaryEnumerator"/> object for the <see cref="T:System.Collections.IDictionary"/> object.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IDictionaryEnumerator"/> object for the <see cref="T:System.Collections.IDictionary"/> object.
/// </returns>
public IDictionaryEnumerator GetEnumerator()
{
return new MyEnumerator(this.implementation.GetEnumerator());
}
/// <summary>
/// Removes the element with the specified key from the <see cref="T:System.Collections.IDictionary"/> object.
/// </summary>
/// <param name="key">The key of the element to remove.</param>
public void Remove(object key)
{
this.implementation.Remove((TKey)key);
}
/// <summary>
/// Copies the elements of the <see cref="T:System.Collections.ICollection"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
/// </summary>
/// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.ICollection"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param>
/// <param name="index">The zero-based index in <paramref name="array"/> at which copying begins.</param>
public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
/// <summary>
/// Wrapper IDictionaryEnumerator.
/// </summary>
private class MyEnumerator : IDictionaryEnumerator
{
private IEnumerator<KeyValuePair<TKey, TValue>> wrapped;
/// <summary>
/// Initializes a new instance of the <see cref="MyEnumerator" /> class.
/// </summary>
/// <param name="wrapped">The wrapped.</param>
public MyEnumerator(IEnumerator<KeyValuePair<TKey, TValue>> wrapped)
{
this.wrapped = wrapped;
}
/// <summary>
/// Gets both the key and the value of the current dictionary entry.
/// </summary>
/// <value></value>
/// <returns>
/// A <see cref="T:System.Collections.DictionaryEntry"/> containing both the key and the value of the current dictionary entry.
/// </returns>
public DictionaryEntry Entry
{
get { return new DictionaryEntry(this.wrapped.Current.Key, this.wrapped.Current.Value); }
}
/// <summary>
/// Gets the key of the current dictionary entry.
/// </summary>
/// <value></value>
/// <returns>
/// The key of the current element of the enumeration.
/// </returns>
public object Key
{
get { return this.wrapped.Current.Key; }
}
/// <summary>
/// Gets the value of the current dictionary entry.
/// </summary>
/// <value></value>
/// <returns>
/// The value of the current element of the enumeration.
/// </returns>
public object Value
{
get { return this.wrapped.Current.Value; }
}
/// <summary>
/// Gets the current element in the collection.
/// </summary>
/// <value></value>
/// <returns>
/// The current element in the collection.
/// </returns>
public object Current
{
get { return this.Entry; }
}
/// <summary>
/// Advances the enumerator to the next element of the collection.
/// </summary>
/// <returns>
/// True if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
/// </returns>
public bool MoveNext()
{
return this.wrapped.MoveNext();
}
/// <summary>
/// Sets the enumerator to its initial position, which is before the first element in the collection.
/// </summary>
public void Reset()
{
this.wrapped.Reset();
}
}
}
}
Jump to Line
Something went wrong with that request. Please try again.