ObservableCollection no longer derives from System.Collections.ObjectModel.Collection, thus it is a real ICollection that does not expose itself as an IList
git-svn-id: file:///srv/devel/repo-conversion/nusu@221 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
a4000c106a
commit
1a215987ac
|
@ -117,30 +117,6 @@ namespace Nuclex.Support.Collections {
|
||||||
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Tests whether items in the collection can be replaced</summary>
|
|
||||||
[Test]
|
|
||||||
public void TestItemReplacement() {
|
|
||||||
this.mockedSubscriber.Expects.Exactly(3).Method(
|
|
||||||
m => m.ItemAdded(null, null)
|
|
||||||
).WithAnyArguments();
|
|
||||||
|
|
||||||
this.observedCollection.Add(1);
|
|
||||||
this.observedCollection.Add(2);
|
|
||||||
this.observedCollection.Add(3);
|
|
||||||
|
|
||||||
this.mockedSubscriber.Expects.One.Method(m => m.ItemRemoved(null, null)).WithAnyArguments();
|
|
||||||
this.mockedSubscriber.Expects.One.Method(m => m.ItemAdded(null, null)).WithAnyArguments();
|
|
||||||
|
|
||||||
// Replace the middle item with something else
|
|
||||||
this.observedCollection[1] = 4;
|
|
||||||
|
|
||||||
Assert.AreEqual(
|
|
||||||
1, this.observedCollection.IndexOf(4)
|
|
||||||
);
|
|
||||||
|
|
||||||
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Tests whether a the list constructor is working</summary>
|
/// <summary>Tests whether a the list constructor is working</summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void TestListConstructor() {
|
public void TestListConstructor() {
|
||||||
|
|
|
@ -19,6 +19,7 @@ License along with this library
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
#if !NO_SPECIALIZED_COLLECTIONS
|
#if !NO_SPECIALIZED_COLLECTIONS
|
||||||
|
@ -30,7 +31,8 @@ namespace Nuclex.Support.Collections {
|
||||||
/// <summary>Collection which fires events when items are added or removed</summary>
|
/// <summary>Collection which fires events when items are added or removed</summary>
|
||||||
/// <typeparam name="TItem">Type of items the collection manages</typeparam>
|
/// <typeparam name="TItem">Type of items the collection manages</typeparam>
|
||||||
public class ObservableCollection<TItem> :
|
public class ObservableCollection<TItem> :
|
||||||
Collection<TItem>,
|
ICollection<TItem>,
|
||||||
|
ICollection,
|
||||||
#if !NO_SPECIALIZED_COLLECTIONS
|
#if !NO_SPECIALIZED_COLLECTIONS
|
||||||
INotifyCollectionChanged,
|
INotifyCollectionChanged,
|
||||||
#endif
|
#endif
|
||||||
|
@ -55,87 +57,98 @@ namespace Nuclex.Support.Collections {
|
||||||
public event NotifyCollectionChangedEventHandler CollectionChanged;
|
public event NotifyCollectionChangedEventHandler CollectionChanged;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>Initializes a new ObservableCollection with no items</summary>
|
||||||
/// Initializes a new instance of the ObservableCollection class that is empty.
|
public ObservableCollection() : this(new Collection<TItem>()) {}
|
||||||
/// </summary>
|
|
||||||
public ObservableCollection() : base() { }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the ObservableCollection class as a wrapper
|
/// Initializes a new ObservableCollection as a wrapper for an existing collection
|
||||||
/// for the specified list.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="list">The list that is wrapped by the new collection.</param>
|
/// <param name="collection">Collection that will be wrapped</param>
|
||||||
/// <exception cref="System.ArgumentNullException">List is null</exception>
|
/// <exception cref="System.ArgumentNullException">List is null</exception>
|
||||||
public ObservableCollection(IList<TItem> list) : base(list) { }
|
public ObservableCollection(ICollection<TItem> collection) {
|
||||||
|
this.typedCollection = collection;
|
||||||
|
this.objectCollection = (collection as ICollection);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Removes all elements from the Collection</summary>
|
/// <summary>Removes all elements from the Collection</summary>
|
||||||
protected override void ClearItems() {
|
public void Clear() {
|
||||||
OnClearing();
|
OnClearing();
|
||||||
base.ClearItems();
|
this.typedCollection.Clear();
|
||||||
OnCleared();
|
OnCleared();
|
||||||
#if !NO_SPECIALIZED_COLLECTIONS
|
#if !NO_SPECIALIZED_COLLECTIONS
|
||||||
OnCollectionChanged(NotifyCollectionChangedAction.Reset, default(TItem), -1);
|
if(CollectionChanged != null) {
|
||||||
|
CollectionChanged(this, CollectionResetEventArgs);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Adds an item to the collection</summary>
|
||||||
|
/// <param name="item">Collection an item will be added to</param>
|
||||||
|
public void Add(TItem item) {
|
||||||
|
this.typedCollection.Add(item);
|
||||||
|
OnAdded(item);
|
||||||
#if !NO_SPECIALIZED_COLLECTIONS
|
#if !NO_SPECIALIZED_COLLECTIONS
|
||||||
/// <summary>Fires the CollectionChanged event</summary>
|
|
||||||
/// <param name="action">Type of change that has occured</param>
|
|
||||||
/// <param name="item">The item that has been added, removed or replaced</param>
|
|
||||||
/// <param name="index">Index of the changed item</param>
|
|
||||||
protected virtual void OnCollectionChanged(
|
|
||||||
NotifyCollectionChangedAction action, TItem item, int index
|
|
||||||
) {
|
|
||||||
if(CollectionChanged != null) {
|
if(CollectionChanged != null) {
|
||||||
CollectionChanged(
|
CollectionChanged(
|
||||||
this, new NotifyCollectionChangedEventArgs(action, item, index)
|
this,
|
||||||
|
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>Determines whether the collection contains the specified item</summary>
|
||||||
/// Inserts an element into the ObservableCollection at the specified index
|
/// <param name="item">Item the collection will be searched for</param>
|
||||||
/// </summary>
|
/// <returns>
|
||||||
/// <param name="index">
|
/// True if the collection contains the specified item, false otherwise
|
||||||
/// The object to insert. The value can be null for reference types.
|
/// </returns>
|
||||||
|
public bool Contains(TItem item) {
|
||||||
|
return this.typedCollection.Contains(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Copies the contents of the collection into an array</summary>
|
||||||
|
/// <param name="array">Array the collection's contents will be copied into</param>
|
||||||
|
/// <param name="arrayIndex">
|
||||||
|
/// Index in the array where the collection's first item will be placed
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="item">The zero-based index at which item should be inserted</param>
|
public void CopyTo(TItem[] array, int arrayIndex) {
|
||||||
protected override void InsertItem(int index, TItem item) {
|
this.typedCollection.CopyTo(array, arrayIndex);
|
||||||
base.InsertItem(index, item);
|
|
||||||
OnAdded(item);
|
|
||||||
#if !NO_SPECIALIZED_COLLECTIONS
|
|
||||||
OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>The total number of items currently in the collection</summary>
|
||||||
/// Removes the element at the specified index of the ObservableCollection
|
public int Count {
|
||||||
/// </summary>
|
get { return this.typedCollection.Count; }
|
||||||
/// <param name="index">The zero-based index of the element to remove</param>
|
}
|
||||||
protected override void RemoveItem(int index) {
|
|
||||||
TItem item = base[index];
|
/// <summary>Whether the collection is read-only</summary>
|
||||||
base.RemoveItem(index);
|
public bool IsReadOnly {
|
||||||
|
get { return this.typedCollection.IsReadOnly; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Removes an item from the collection</summary>
|
||||||
|
/// <param name="item">Item that will be removed from the collection</param>
|
||||||
|
/// <returns>True if the item was found and removed, false otherwise</returns>
|
||||||
|
public bool Remove(TItem item) {
|
||||||
|
bool wasRemoved = this.typedCollection.Remove(item);
|
||||||
|
if(wasRemoved) {
|
||||||
OnRemoved(item);
|
OnRemoved(item);
|
||||||
#if !NO_SPECIALIZED_COLLECTIONS
|
#if !NO_SPECIALIZED_COLLECTIONS
|
||||||
OnCollectionChanged(NotifyCollectionChangedAction.Remove, item, index);
|
if(CollectionChanged != null) {
|
||||||
|
CollectionChanged(
|
||||||
|
this,
|
||||||
|
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)
|
||||||
|
);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Replaces the element at the specified index</summary>
|
return wasRemoved;
|
||||||
/// <param name="index">
|
}
|
||||||
/// The new value for the element at the specified index. The value can be null
|
|
||||||
/// for reference types
|
/// <summary>Returns an enumerator for the items in the collection</summary>
|
||||||
/// </param>
|
/// <returns>An enumeration for the items in the collection</returns>
|
||||||
/// <param name="item">The zero-based index of the element to replace</param>
|
public IEnumerator<TItem> GetEnumerator() {
|
||||||
protected override void SetItem(int index, TItem item) {
|
return this.typedCollection.GetEnumerator();
|
||||||
TItem oldItem = base[index];
|
|
||||||
base.SetItem(index, item);
|
|
||||||
OnRemoved(oldItem);
|
|
||||||
OnAdded(item);
|
|
||||||
#if !NO_SPECIALIZED_COLLECTIONS
|
|
||||||
OnCollectionChanged(NotifyCollectionChangedAction.Replace, item, index);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Fires the 'ItemAdded' event</summary>
|
/// <summary>Fires the 'ItemAdded' event</summary>
|
||||||
|
@ -164,6 +177,52 @@ namespace Nuclex.Support.Collections {
|
||||||
Cleared(this, EventArgs.Empty);
|
Cleared(this, EventArgs.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region IEnumerable implementation
|
||||||
|
|
||||||
|
/// <summary>Returns an enumerator for the items in the collection</summary>
|
||||||
|
/// <returns>An enumeration for the items in the collection</returns>
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() {
|
||||||
|
return this.objectCollection.GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // IEnumerable implementation
|
||||||
|
|
||||||
|
#region ICollection implementation
|
||||||
|
|
||||||
|
/// <summary>Copies the contents of the collection into an array</summary>
|
||||||
|
/// <param name="array">Array the collection's contents will be copied into</param>
|
||||||
|
/// <param name="arrayIndex">
|
||||||
|
/// Index in the array where the collection's first item will be placed
|
||||||
|
/// </param>
|
||||||
|
void ICollection.CopyTo(Array array, int arrayIndex) {
|
||||||
|
this.objectCollection.CopyTo(array, arrayIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Whether the collection synchronizes accesses from multiple threads</summary>
|
||||||
|
bool ICollection.IsSynchronized {
|
||||||
|
get { return this.objectCollection.IsSynchronized; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Synchronization root used to synchronize threads accessing the collection
|
||||||
|
/// </summary>
|
||||||
|
object ICollection.SyncRoot {
|
||||||
|
get { return this.objectCollection.SyncRoot; }
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // IEnumerable implementation
|
||||||
|
|
||||||
|
#if !NO_SPECIALIZED_COLLECTIONS
|
||||||
|
/// <summary>Fixed event args used to notify that the collection has reset</summary>
|
||||||
|
private static readonly NotifyCollectionChangedEventArgs CollectionResetEventArgs =
|
||||||
|
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/// <summary>The wrapped collection under its type-safe interface</summary>
|
||||||
|
private ICollection<TItem> typedCollection;
|
||||||
|
/// <summary>The wrapped collection under its object interface</summary>
|
||||||
|
private ICollection objectCollection;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Nuclex.Support.Collections
|
} // namespace Nuclex.Support.Collections
|
||||||
|
|
|
@ -364,9 +364,9 @@ namespace Nuclex.Support.Collections {
|
||||||
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
|
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// <summary>The wrapped List under its type-safe interface</summary>
|
/// <summary>The wrapped list under its type-safe interface</summary>
|
||||||
private IList<TItem> typedList;
|
private IList<TItem> typedList;
|
||||||
/// <summary>The wrapped List under its object interface</summary>
|
/// <summary>The wrapped list under its object interface</summary>
|
||||||
private IList objectList;
|
private IList objectList;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user