Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace sirhc.Collections.Generic
- {
- public class ObservableList<T> : IList<T>
- {
- readonly ObservableList()
- {
- internalList = List<T>();
- }
- public ObservableList(IList<T> list)
- {
- if (list == null)
- throw new ArgumentException("list");
- internalList = list;
- }
- public ObservableList(IEnumerable<T> collection)
- {
- if (collection == null)
- throw new ArgumentNullException("collection");
- internalList = new List<T>(collection);
- }
- public int IndexOf(T item)
- {
- rpublic int IndexOf(T item)
- {
- return internalList.IndexOf(item);
- }
- public void Insert(int index, T item)
- {
- internalList.Insert(index, item);
- OnListChanged(new ListChangedEventArgs(index, item));
- }
- public void RemoveAt(int index)
- {
- var item = internalList[index];
- internalList.RemoveAt(index);
- OnListChanged(new ListChangedEventArgs(index, item));
- }
- public T this[int index]
- {
- get { return internalList[index]; }
- set
- {
- if (internalList[index].Equals(value)) return;
- internalList[index] = value;
- OnListChanged(new ListChangedEventArgs(index, value));
- }
- }
- public void Add(T item)
- {
- internalList.Add(item);
- OnListChanged(new ListChangedEventArgs(internalList.IndexOf(item), item));
- }
- public void Clear()
- {
- internalList.Clear();
- OnListCleared(new EventArgs());
- }
- public bool Contains(T item)
- {
- return internalList.Contains(item);
- }
- public void CopyTo(T[] array, int arrayIndex)
- {
- internalList.CopyTo(array, arrayIndex);
- }
- public int Count
- {
- get { return internalList.Count; }
- }
- public bool IsReadOnly
- {
- get { return internalList.IsReadOnly; }
- }
- public bool Remove(T item)
- {
- lock (this)
- {
- var index = internalList.IndexOf(item);
- if (internalList.Remove(item))
- {
- OnListChanged(new ListChangedEventArgs(index, item));
- return true;
- }
- }
- return false;
- }
- public IEnumerator<T> GetEnumerator()
- {
- return internalList.GetEnumerator();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return ((IEnumerable)internalList).GetEnumerator();
- }
- public event EventHandler<ListChangedEventArgs> ListChanged = delegate { };
- public event EventHandler ListCleared = delegate { };
- protected virtual void OnListChanged(ListChangedEventArgs e)
- {
- ListChanged(this, e);
- }
- protected virtual void OnListCleared(EventArgs e)
- {
- ListCleared(this, e);
- }
- public class ListChangedEventArgs : EventArgs
- {
- readonly int index;
- readonly T item;
- internal ListChangedEventArgs(int index, T item)
- {
- this.index = index;
- this.item = item;
- }
- public int Index
- {
- get { return index; }
- }
- public T Item
- {
- get { return item; }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment