CGC_Codes

Utility Sample

Feb 23rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.84 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace sirhc.Collections.Generic
  6. {
  7.     public class ObservableList<T> : IList<T>
  8.     {
  9.         readonly ObservableList()
  10.         {
  11.             internalList = List<T>();
  12.         }
  13.  
  14.         public ObservableList(IList<T> list)
  15.         {
  16.             if (list == null)
  17.                 throw new ArgumentException("list");
  18.             internalList = list;
  19.         }
  20.  
  21.         public ObservableList(IEnumerable<T> collection)
  22.         {
  23.             if (collection == null)
  24.                 throw new ArgumentNullException("collection");
  25.  
  26.             internalList = new List<T>(collection);
  27.         }
  28.  
  29.         public int IndexOf(T item)
  30.         {
  31.             rpublic int IndexOf(T item)
  32.         {
  33.                 return internalList.IndexOf(item);
  34.             }
  35.  
  36.         public void Insert(int index, T item)
  37.         {
  38.             internalList.Insert(index, item);
  39.             OnListChanged(new ListChangedEventArgs(index, item));
  40.         }
  41.  
  42.         public void RemoveAt(int index)
  43.         {
  44.             var item = internalList[index];
  45.             internalList.RemoveAt(index);
  46.             OnListChanged(new ListChangedEventArgs(index, item));
  47.         }
  48.  
  49.         public T this[int index]
  50.         {
  51.             get { return internalList[index]; }
  52.             set
  53.             {
  54.                 if (internalList[index].Equals(value)) return;
  55.  
  56.                 internalList[index] = value;
  57.                 OnListChanged(new ListChangedEventArgs(index, value));
  58.             }
  59.         }
  60.  
  61.         public void Add(T item)
  62.         {
  63.             internalList.Add(item);
  64.             OnListChanged(new ListChangedEventArgs(internalList.IndexOf(item), item));
  65.         }
  66.  
  67.         public void Clear()
  68.         {
  69.             internalList.Clear();
  70.             OnListCleared(new EventArgs());
  71.         }
  72.  
  73.         public bool Contains(T item)
  74.         {
  75.             return internalList.Contains(item);
  76.         }
  77.  
  78.         public void CopyTo(T[] array, int arrayIndex)
  79.         {
  80.             internalList.CopyTo(array, arrayIndex);
  81.         }
  82.  
  83.         public int Count
  84.         {
  85.             get { return internalList.Count; }
  86.         }
  87.  
  88.         public bool IsReadOnly
  89.         {
  90.             get { return internalList.IsReadOnly; }
  91.         }
  92.  
  93.         public bool Remove(T item)
  94.         {
  95.             lock (this)
  96.             {
  97.                 var index = internalList.IndexOf(item);
  98.                 if (internalList.Remove(item))
  99.                 {
  100.                     OnListChanged(new ListChangedEventArgs(index, item));
  101.                     return true;
  102.                 }
  103.             }
  104.             return false;
  105.         }
  106.  
  107.         public IEnumerator<T> GetEnumerator()
  108.         {
  109.             return internalList.GetEnumerator();
  110.         }
  111.  
  112.         IEnumerator IEnumerable.GetEnumerator()
  113.         {
  114.             return ((IEnumerable)internalList).GetEnumerator();
  115.         }
  116.  
  117.         public event EventHandler<ListChangedEventArgs> ListChanged = delegate { };
  118.         public event EventHandler ListCleared = delegate { };
  119.  
  120.         protected virtual void OnListChanged(ListChangedEventArgs e)
  121.         {
  122.             ListChanged(this, e);
  123.         }
  124.  
  125.         protected virtual void OnListCleared(EventArgs e)
  126.         {
  127.             ListCleared(this, e);
  128.         }
  129.  
  130.         public class ListChangedEventArgs : EventArgs
  131.         {
  132.             readonly int index;
  133.             readonly T item;
  134.  
  135.             internal ListChangedEventArgs(int index, T item)
  136.             {
  137.                 this.index = index;
  138.                 this.item = item;
  139.             }
  140.  
  141.             public int Index
  142.             {
  143.                 get { return index; }
  144.             }
  145.  
  146.             public T Item
  147.             {
  148.                 get { return item; }
  149.             }
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment