2009-01-26 20:05:32 +00:00
|
|
|
|
#region CPL License
|
|
|
|
|
/*
|
|
|
|
|
Nuclex Framework
|
2012-02-29 16:27:43 +00:00
|
|
|
|
Copyright (C) 2002-2012 Nuclex Development Labs
|
2009-01-26 20:05:32 +00:00
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the IBM Common Public License as
|
|
|
|
|
published by the IBM Corporation; either version 1.0 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
IBM Common Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the IBM Common Public
|
|
|
|
|
License along with this library
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
2011-07-04 22:21:57 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
#if !NO_SPECIALIZED_COLLECTIONS
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
#endif
|
2009-01-26 20:05:32 +00:00
|
|
|
|
using System.Runtime.Serialization;
|
|
|
|
|
|
|
|
|
|
namespace Nuclex.Support.Collections {
|
|
|
|
|
|
|
|
|
|
/// <summary>A dictionary that sneds out change notifications</summary>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
/// <typeparam name="TKey">Type of the keys used in the dictionary</typeparam>
|
|
|
|
|
/// <typeparam name="TValue">Type of the values used in the dictionary</typeparam>
|
2010-09-17 01:43:00 +00:00
|
|
|
|
#if !NO_SERIALIZATION
|
2009-01-26 20:05:32 +00:00
|
|
|
|
[Serializable]
|
2010-09-17 01:43:00 +00:00
|
|
|
|
#endif
|
2011-07-04 22:21:57 +00:00
|
|
|
|
public class ObservableDictionary<TKey, TValue> :
|
2009-06-26 20:17:59 +00:00
|
|
|
|
#if !NO_SERIALIZATION
|
2012-03-01 13:51:04 +00:00
|
|
|
|
ISerializable,
|
2009-01-26 20:05:32 +00:00
|
|
|
|
IDeserializationCallback,
|
|
|
|
|
#endif
|
2012-03-01 13:51:04 +00:00
|
|
|
|
IDictionary<TKey, TValue>,
|
2009-03-19 19:34:11 +00:00
|
|
|
|
IDictionary,
|
2011-07-04 22:21:57 +00:00
|
|
|
|
#if !NO_SPECIALIZED_COLLECTIONS
|
2012-03-01 13:51:04 +00:00
|
|
|
|
INotifyCollectionChanged,
|
2011-07-04 22:21:57 +00:00
|
|
|
|
#endif
|
2012-03-01 13:51:04 +00:00
|
|
|
|
IObservableCollection<KeyValuePair<TKey, TValue>> {
|
2009-01-26 20:05:32 +00:00
|
|
|
|
|
2009-06-26 20:17:59 +00:00
|
|
|
|
#if !NO_SERIALIZATION
|
2009-01-26 20:05:32 +00:00
|
|
|
|
#region class SerializedDictionary
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Dictionary wrapped used to reconstruct a serialized read only dictionary
|
|
|
|
|
/// </summary>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
private class SerializedDictionary : Dictionary<TKey, TValue> {
|
2009-01-26 20:05:32 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the System.WeakReference class, using deserialized
|
|
|
|
|
/// data from the specified serialization and stream objects.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="info">
|
|
|
|
|
/// An object that holds all the data needed to serialize or deserialize the
|
|
|
|
|
/// current System.WeakReference object.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="context">
|
|
|
|
|
/// (Reserved) Describes the source and destination of the serialized stream
|
|
|
|
|
/// specified by info.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">
|
|
|
|
|
/// The info parameter is null.
|
|
|
|
|
/// </exception>
|
|
|
|
|
public SerializedDictionary(SerializationInfo info, StreamingContext context) :
|
|
|
|
|
base(info, context) { }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-26 20:06:41 +00:00
|
|
|
|
#endregion // class SerializedDictionary
|
2011-07-04 22:21:57 +00:00
|
|
|
|
#endif // !NO_SERIALIZATION
|
2009-01-26 20:05:32 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>Raised when an item has been added to the dictionary</summary>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
public event EventHandler<ItemEventArgs<KeyValuePair<TKey, TValue>>> ItemAdded;
|
2009-01-26 20:05:32 +00:00
|
|
|
|
/// <summary>Raised when an item is removed from the dictionary</summary>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
public event EventHandler<ItemEventArgs<KeyValuePair<TKey, TValue>>> ItemRemoved;
|
2012-03-01 12:52:12 +00:00
|
|
|
|
/// <summary>Raised when an item is replaced in the collection</summary>
|
|
|
|
|
public event EventHandler<ItemReplaceEventArgs<KeyValuePair<TKey, TValue>>> ItemReplaced;
|
2009-01-26 20:05:32 +00:00
|
|
|
|
/// <summary>Raised when the dictionary is about to be cleared</summary>
|
|
|
|
|
public event EventHandler Clearing;
|
2009-10-12 19:19:58 +00:00
|
|
|
|
/// <summary>Raised when the dictionary has been cleared</summary>
|
|
|
|
|
public event EventHandler Cleared;
|
2009-01-26 20:05:32 +00:00
|
|
|
|
|
2011-07-04 22:21:57 +00:00
|
|
|
|
#if !NO_SPECIALIZED_COLLECTIONS
|
|
|
|
|
/// <summary>Called when the collection has changed</summary>
|
|
|
|
|
public event NotifyCollectionChangedEventHandler CollectionChanged;
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-01-26 20:05:32 +00:00
|
|
|
|
/// <summary>Initializes a new observable dictionary</summary>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
public ObservableDictionary() : this(new Dictionary<TKey, TValue>()) { }
|
2009-01-26 20:05:32 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new observable Dictionary wrapper</summary>
|
|
|
|
|
/// <param name="dictionary">Dictionary that will be wrapped</param>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
public ObservableDictionary(IDictionary<TKey, TValue> dictionary) {
|
2009-01-26 20:05:32 +00:00
|
|
|
|
this.typedDictionary = dictionary;
|
|
|
|
|
this.objectDictionary = (this.typedDictionary as IDictionary);
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-26 20:17:59 +00:00
|
|
|
|
#if !NO_SERIALIZATION
|
|
|
|
|
|
2009-01-26 20:05:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the System.WeakReference class, using deserialized
|
|
|
|
|
/// data from the specified serialization and stream objects.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="info">
|
|
|
|
|
/// An object that holds all the data needed to serialize or deserialize the
|
|
|
|
|
/// current System.WeakReference object.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="context">
|
|
|
|
|
/// (Reserved) Describes the source and destination of the serialized stream
|
|
|
|
|
/// specified by info.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">
|
|
|
|
|
/// The info parameter is null.
|
|
|
|
|
/// </exception>
|
|
|
|
|
protected ObservableDictionary(SerializationInfo info, StreamingContext context) :
|
|
|
|
|
this(new SerializedDictionary(info, context)) { }
|
2009-06-26 20:17:59 +00:00
|
|
|
|
|
|
|
|
|
#endif // !NO_SERIALIZATION
|
2009-01-26 20:05:32 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>Whether the directory is write-protected</summary>
|
|
|
|
|
public bool IsReadOnly {
|
|
|
|
|
get { return this.typedDictionary.IsReadOnly; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether the specified KeyValuePair is contained in the Dictionary
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">KeyValuePair that will be checked for</param>
|
|
|
|
|
/// <returns>True if the provided KeyValuePair was contained in the Dictionary</returns>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
public bool Contains(KeyValuePair<TKey, TValue> item) {
|
2009-01-26 20:05:32 +00:00
|
|
|
|
return this.typedDictionary.Contains(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Determines whether the Dictionary contains the specified key</summary>
|
|
|
|
|
/// <param name="key">Key that will be checked for</param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// True if an entry with the specified key was contained in the Dictionary
|
|
|
|
|
/// </returns>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
public bool ContainsKey(TKey key) {
|
2009-01-26 20:05:32 +00:00
|
|
|
|
return this.typedDictionary.ContainsKey(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Copies the contents of the Dictionary into an array</summary>
|
|
|
|
|
/// <param name="array">Array the Dictionary will be copied into</param>
|
|
|
|
|
/// <param name="arrayIndex">
|
|
|
|
|
/// Starting index at which to begin filling the destination array
|
|
|
|
|
/// </param>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
|
2009-01-26 20:05:32 +00:00
|
|
|
|
this.typedDictionary.CopyTo(array, arrayIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Number of elements contained in the Dictionary</summary>
|
|
|
|
|
public int Count {
|
|
|
|
|
get { return this.typedDictionary.Count; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Creates a new enumerator for the Dictionary</summary>
|
|
|
|
|
/// <returns>The new Dictionary enumerator</returns>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
|
2009-01-26 20:05:32 +00:00
|
|
|
|
return this.typedDictionary.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Collection of all keys contained in the Dictionary</summary>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
public ICollection<TKey> Keys {
|
2009-01-26 20:05:32 +00:00
|
|
|
|
get { return this.typedDictionary.Keys; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Collection of all values contained in the Dictionary</summary>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
public ICollection<TValue> Values {
|
2009-01-26 20:05:32 +00:00
|
|
|
|
get { return this.typedDictionary.Values; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Attempts to retrieve the item with the specified key from the Dictionary
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="key">Key of the item to attempt to retrieve</param>
|
|
|
|
|
/// <param name="value">
|
|
|
|
|
/// Output parameter that will receive the key upon successful completion
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// True if the item was found and has been placed in the output parameter
|
|
|
|
|
/// </returns>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
public bool TryGetValue(TKey key, out TValue value) {
|
2009-01-26 20:05:32 +00:00
|
|
|
|
return this.typedDictionary.TryGetValue(key, out value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Accesses an item in the Dictionary by its key</summary>
|
|
|
|
|
/// <param name="key">Key of the item that will be accessed</param>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
public TValue this[TKey key] {
|
2009-01-26 20:05:32 +00:00
|
|
|
|
get { return this.typedDictionary[key]; }
|
|
|
|
|
set {
|
|
|
|
|
bool removed;
|
2011-07-04 22:21:57 +00:00
|
|
|
|
TValue oldValue;
|
2009-01-26 20:05:32 +00:00
|
|
|
|
removed = this.typedDictionary.TryGetValue(key, out oldValue);
|
|
|
|
|
|
|
|
|
|
this.typedDictionary[key] = value;
|
|
|
|
|
|
|
|
|
|
if(removed) {
|
2012-03-01 12:52:12 +00:00
|
|
|
|
OnReplaced(
|
|
|
|
|
new KeyValuePair<TKey, TValue>(key, oldValue),
|
|
|
|
|
new KeyValuePair<TKey, TValue>(key, value)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
OnAdded(new KeyValuePair<TKey, TValue>(key, value));
|
2009-01-26 20:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Inserts an item into the Dictionary</summary>
|
|
|
|
|
/// <param name="key">Key under which to add the new item</param>
|
|
|
|
|
/// <param name="value">Item that will be added to the Dictionary</param>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
public void Add(TKey key, TValue value) {
|
2009-01-26 20:05:32 +00:00
|
|
|
|
this.typedDictionary.Add(key, value);
|
2011-07-04 22:21:57 +00:00
|
|
|
|
OnAdded(new KeyValuePair<TKey, TValue>(key, value));
|
2009-01-26 20:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Removes the item with the specified key from the Dictionary</summary>
|
|
|
|
|
/// <param name="key">Key of the elementes that will be removed</param>
|
|
|
|
|
/// <returns>True if an item with the specified key was found and removed</returns>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
public bool Remove(TKey key) {
|
|
|
|
|
TValue oldValue;
|
2009-01-26 20:05:32 +00:00
|
|
|
|
this.typedDictionary.TryGetValue(key, out oldValue);
|
|
|
|
|
|
|
|
|
|
bool removed = this.typedDictionary.Remove(key);
|
|
|
|
|
if(removed) {
|
2011-07-04 22:21:57 +00:00
|
|
|
|
OnRemoved(new KeyValuePair<TKey, TValue>(key, oldValue));
|
2009-01-26 20:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
return removed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Removes all items from the Dictionary</summary>
|
|
|
|
|
public void Clear() {
|
|
|
|
|
OnClearing();
|
|
|
|
|
this.typedDictionary.Clear();
|
2009-10-12 19:19:58 +00:00
|
|
|
|
OnCleared();
|
2009-01-26 20:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Fires the 'ItemAdded' event</summary>
|
|
|
|
|
/// <param name="item">Item that has been added to the collection</param>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
protected virtual void OnAdded(KeyValuePair<TKey, TValue> item) {
|
2009-01-26 20:05:32 +00:00
|
|
|
|
if(ItemAdded != null)
|
2011-07-04 22:21:57 +00:00
|
|
|
|
ItemAdded(this, new ItemEventArgs<KeyValuePair<TKey, TValue>>(item));
|
2011-08-24 11:33:22 +00:00
|
|
|
|
|
2011-07-04 22:21:57 +00:00
|
|
|
|
#if !NO_SPECIALIZED_COLLECTIONS
|
2012-03-01 12:52:12 +00:00
|
|
|
|
if(CollectionChanged != null) {
|
2011-07-04 22:21:57 +00:00
|
|
|
|
CollectionChanged(
|
2012-03-01 12:52:12 +00:00
|
|
|
|
this,
|
|
|
|
|
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)
|
2011-07-04 22:21:57 +00:00
|
|
|
|
);
|
2012-03-01 12:52:12 +00:00
|
|
|
|
}
|
2011-07-04 22:21:57 +00:00
|
|
|
|
#endif
|
2009-01-26 20:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Fires the 'ItemRemoved' event</summary>
|
|
|
|
|
/// <param name="item">Item that has been removed from the collection</param>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
protected virtual void OnRemoved(KeyValuePair<TKey, TValue> item) {
|
2012-03-01 12:52:12 +00:00
|
|
|
|
if(ItemRemoved != null) {
|
2011-07-04 22:21:57 +00:00
|
|
|
|
ItemRemoved(this, new ItemEventArgs<KeyValuePair<TKey, TValue>>(item));
|
2012-03-01 12:52:12 +00:00
|
|
|
|
}
|
|
|
|
|
#if !NO_SPECIALIZED_COLLECTIONS
|
|
|
|
|
if(CollectionChanged != null) {
|
|
|
|
|
CollectionChanged(
|
|
|
|
|
this,
|
|
|
|
|
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2011-08-24 11:33:22 +00:00
|
|
|
|
|
2012-03-01 12:52:12 +00:00
|
|
|
|
/// <summary>Fires the 'ItemReplaced' event</summary>
|
|
|
|
|
/// <param name="oldItem">Item that has been replaced in the collection</param>
|
|
|
|
|
/// <param name="newItem">Item with which the original item was replaced</param>
|
|
|
|
|
protected virtual void OnReplaced(
|
|
|
|
|
KeyValuePair<TKey, TValue> oldItem, KeyValuePair<TKey, TValue> newItem
|
|
|
|
|
) {
|
|
|
|
|
if(ItemReplaced != null) {
|
|
|
|
|
ItemReplaced(
|
|
|
|
|
this,
|
|
|
|
|
new ItemReplaceEventArgs<KeyValuePair<TKey, TValue>>(oldItem, newItem)
|
|
|
|
|
);
|
|
|
|
|
}
|
2011-07-04 22:21:57 +00:00
|
|
|
|
#if !NO_SPECIALIZED_COLLECTIONS
|
2012-03-01 12:52:12 +00:00
|
|
|
|
if(CollectionChanged != null) {
|
2011-07-04 22:21:57 +00:00
|
|
|
|
CollectionChanged(
|
|
|
|
|
this, new NotifyCollectionChangedEventArgs(
|
2012-03-01 12:52:12 +00:00
|
|
|
|
NotifyCollectionChangedAction.Replace, newItem, oldItem
|
2011-07-04 22:21:57 +00:00
|
|
|
|
)
|
|
|
|
|
);
|
2012-03-01 12:52:12 +00:00
|
|
|
|
}
|
2011-07-04 22:21:57 +00:00
|
|
|
|
#endif
|
2009-01-26 20:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Fires the 'Clearing' event</summary>
|
|
|
|
|
protected virtual void OnClearing() {
|
2012-03-01 13:51:04 +00:00
|
|
|
|
if(Clearing != null) {
|
2009-01-26 20:05:32 +00:00
|
|
|
|
Clearing(this, EventArgs.Empty);
|
2012-03-01 13:51:04 +00:00
|
|
|
|
}
|
2009-01-26 20:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-10-12 19:19:58 +00:00
|
|
|
|
/// <summary>Fires the 'Cleared' event</summary>
|
|
|
|
|
protected virtual void OnCleared() {
|
2012-03-01 13:51:04 +00:00
|
|
|
|
if(Cleared != null) {
|
2009-10-12 19:19:58 +00:00
|
|
|
|
Cleared(this, EventArgs.Empty);
|
2012-03-01 13:51:04 +00:00
|
|
|
|
}
|
2011-07-04 22:21:57 +00:00
|
|
|
|
#if !NO_SPECIALIZED_COLLECTIONS
|
2012-03-01 12:52:12 +00:00
|
|
|
|
if(CollectionChanged != null) {
|
|
|
|
|
CollectionChanged(this, Constants.NotifyCollectionResetEventArgs);
|
|
|
|
|
}
|
2011-07-04 22:21:57 +00:00
|
|
|
|
#endif
|
2009-10-12 19:19:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-01-26 20:05:32 +00:00
|
|
|
|
#region IEnumerable implementation
|
|
|
|
|
|
|
|
|
|
/// <summary>Returns a new object enumerator for the Dictionary</summary>
|
|
|
|
|
/// <returns>The new object enumerator</returns>
|
|
|
|
|
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
|
|
|
|
|
return (this.typedDictionary as IEnumerable).GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IDictionary implementation
|
|
|
|
|
|
|
|
|
|
/// <summary>Adds an item into the Dictionary</summary>
|
|
|
|
|
/// <param name="key">Key under which the item will be added</param>
|
|
|
|
|
/// <param name="value">Item that will be added</param>
|
|
|
|
|
void IDictionary.Add(object key, object value) {
|
|
|
|
|
this.objectDictionary.Add(key, value);
|
2011-07-04 22:21:57 +00:00
|
|
|
|
OnAdded(new KeyValuePair<TKey, TValue>((TKey)key, (TValue)value));
|
2009-01-26 20:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Determines whether the specified key exists in the Dictionary</summary>
|
|
|
|
|
/// <param name="key">Key that will be checked for</param>
|
|
|
|
|
/// <returns>True if an item with the specified key exists in the Dictionary</returns>
|
|
|
|
|
bool IDictionary.Contains(object key) {
|
|
|
|
|
return this.objectDictionary.Contains(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Returns a new entry enumerator for the dictionary</summary>
|
|
|
|
|
/// <returns>The new entry enumerator</returns>
|
|
|
|
|
IDictionaryEnumerator IDictionary.GetEnumerator() {
|
|
|
|
|
return this.objectDictionary.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Whether the size of the Dictionary is fixed</summary>
|
|
|
|
|
bool IDictionary.IsFixedSize {
|
|
|
|
|
get { return this.objectDictionary.IsFixedSize; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Returns a collection of all keys in the Dictionary</summary>
|
|
|
|
|
ICollection IDictionary.Keys {
|
|
|
|
|
get { return this.objectDictionary.Keys; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Returns a collection of all values stored in the Dictionary</summary>
|
|
|
|
|
ICollection IDictionary.Values {
|
|
|
|
|
get { return this.objectDictionary.Values; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Removes an item from the Dictionary</summary>
|
|
|
|
|
/// <param name="key">Key of the item that will be removed</param>
|
|
|
|
|
void IDictionary.Remove(object key) {
|
2011-07-04 22:21:57 +00:00
|
|
|
|
TValue value;
|
|
|
|
|
bool removed = this.typedDictionary.TryGetValue((TKey)key, out value);
|
2009-01-26 20:05:32 +00:00
|
|
|
|
this.objectDictionary.Remove(key);
|
|
|
|
|
if(removed) {
|
2011-07-04 22:21:57 +00:00
|
|
|
|
OnRemoved(new KeyValuePair<TKey, TValue>((TKey)key, (TValue)value));
|
2009-01-26 20:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Accesses an item in the Dictionary by its key</summary>
|
|
|
|
|
/// <param name="key">Key of the item that will be accessed</param>
|
|
|
|
|
/// <returns>The item with the specified key</returns>
|
|
|
|
|
object IDictionary.this[object key] {
|
|
|
|
|
get { return this.objectDictionary[key]; }
|
|
|
|
|
set {
|
|
|
|
|
bool removed;
|
2011-07-04 22:21:57 +00:00
|
|
|
|
TValue oldValue;
|
|
|
|
|
removed = this.typedDictionary.TryGetValue((TKey)key, out oldValue);
|
2009-01-26 20:05:32 +00:00
|
|
|
|
|
|
|
|
|
this.objectDictionary[key] = value;
|
|
|
|
|
|
|
|
|
|
if(removed) {
|
2011-07-04 22:21:57 +00:00
|
|
|
|
OnRemoved(new KeyValuePair<TKey, TValue>((TKey)key, oldValue));
|
2009-01-26 20:05:32 +00:00
|
|
|
|
}
|
2011-07-04 22:21:57 +00:00
|
|
|
|
OnAdded(new KeyValuePair<TKey, TValue>((TKey)key, (TValue)value));
|
2009-01-26 20:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ICollection<> implementation
|
|
|
|
|
|
|
|
|
|
/// <summary>Inserts an already prepared element into the Dictionary</summary>
|
|
|
|
|
/// <param name="item">Prepared element that will be added to the Dictionary</param>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
void ICollection<KeyValuePair<TKey, TValue>>.Add(
|
|
|
|
|
KeyValuePair<TKey, TValue> item
|
2009-01-26 20:05:32 +00:00
|
|
|
|
) {
|
|
|
|
|
this.typedDictionary.Add(item);
|
|
|
|
|
OnAdded(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Removes all items from the Dictionary</summary>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
void ICollection<KeyValuePair<TKey, TValue>>.Clear() {
|
2009-01-26 20:05:32 +00:00
|
|
|
|
OnClearing();
|
|
|
|
|
this.typedDictionary.Clear();
|
2009-10-12 19:19:58 +00:00
|
|
|
|
OnCleared();
|
2009-01-26 20:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Removes all items from the Dictionary</summary>
|
|
|
|
|
/// <param name="itemToRemove">Item that will be removed from the Dictionary</param>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(
|
|
|
|
|
KeyValuePair<TKey, TValue> itemToRemove
|
2009-01-26 20:05:32 +00:00
|
|
|
|
) {
|
|
|
|
|
bool removed = this.typedDictionary.Remove(itemToRemove);
|
|
|
|
|
if(removed) {
|
|
|
|
|
OnRemoved(itemToRemove);
|
|
|
|
|
}
|
|
|
|
|
return removed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ICollection implementation
|
|
|
|
|
|
|
|
|
|
/// <summary>Copies the contents of the Dictionary into an array</summary>
|
|
|
|
|
/// <param name="array">Array the Dictionary contents will be copied into</param>
|
|
|
|
|
/// <param name="index">
|
|
|
|
|
/// Starting index at which to begin filling the destination array
|
|
|
|
|
/// </param>
|
|
|
|
|
void ICollection.CopyTo(Array array, int index) {
|
|
|
|
|
this.objectDictionary.CopyTo(array, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Whether the Dictionary is synchronized for multi-threaded usage</summary>
|
|
|
|
|
bool ICollection.IsSynchronized {
|
|
|
|
|
get { return this.objectDictionary.IsSynchronized; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Synchronization root on which the Dictionary locks</summary>
|
|
|
|
|
object ICollection.SyncRoot {
|
|
|
|
|
get { return this.objectDictionary.SyncRoot; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2009-06-26 20:17:59 +00:00
|
|
|
|
#if !NO_SERIALIZATION
|
2009-01-26 20:05:32 +00:00
|
|
|
|
#region ISerializable implementation
|
|
|
|
|
|
|
|
|
|
/// <summary>Serializes the Dictionary</summary>
|
|
|
|
|
/// <param name="info">
|
|
|
|
|
/// Provides the container into which the Dictionary will serialize itself
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="context">
|
|
|
|
|
/// Contextual informations about the serialization environment
|
|
|
|
|
/// </param>
|
|
|
|
|
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) {
|
|
|
|
|
(this.typedDictionary as ISerializable).GetObjectData(info, context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Called after all objects have been successfully deserialized</summary>
|
|
|
|
|
/// <param name="sender">Nicht unterstützt</param>
|
|
|
|
|
void IDeserializationCallback.OnDeserialization(object sender) {
|
|
|
|
|
(this.typedDictionary as IDeserializationCallback).OnDeserialization(sender);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2009-06-26 20:17:59 +00:00
|
|
|
|
#endif //!NO_SERIALIZATION
|
2009-01-26 20:05:32 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>The wrapped Dictionary under its type-safe interface</summary>
|
2011-07-04 22:21:57 +00:00
|
|
|
|
private IDictionary<TKey, TValue> typedDictionary;
|
2009-01-26 20:05:32 +00:00
|
|
|
|
/// <summary>The wrapped Dictionary under its object interface</summary>
|
|
|
|
|
private IDictionary objectDictionary;
|
2011-07-04 22:21:57 +00:00
|
|
|
|
|
2009-01-26 20:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support.Collections
|