Advertisement
Guest User

Untitled

a guest
May 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 2.33 KB | None | 0 0
  1.  
  2. public class DerivedObservableCollection<TBase, TValue> : ICollection<TValue>, System.Collections.Specialized.INotifyCollectionChanged
  3. {
  4.     private ICollection<TBase> baseCollection;
  5.     private Func<TBase, TValue> selector;
  6.  
  7.     public event System.Collections.Specialized.NotifyCollectionChangedEventHandler CollectionChanged;
  8.  
  9.     public DerivedObservableCollection(ICollection<TBase> baseCollection, Func<TBase, TValue> selector)
  10.     {
  11.         this.baseCollection = baseCollection;
  12.         this.selector = selector;
  13.  
  14.         var notifyCollectionChanged = (System.Collections.Specialized.INotifyCollectionChanged)this.baseCollection;
  15.         notifyCollectionChanged.CollectionChanged += baseCollection_CollectionChanged;
  16.     }
  17.  
  18.     void baseCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  19.     {
  20.         OnCollectionChanged(e);
  21.     }
  22.  
  23.     protected void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  24.     {
  25.         // Convert 'e' so it represents TValue instead of TBase here.
  26.  
  27.         if (CollectionChanged != null)
  28.             CollectionChanged(this, e);
  29.     }
  30.  
  31.     #region ICollection<TValue> Members
  32.  
  33.     public void Add(TValue item)
  34.     {
  35.         throw new NotSupportedException();
  36.     }
  37.  
  38.     public void Clear()
  39.     {
  40.         throw new NotSupportedException();
  41.     }
  42.  
  43.     public bool Contains(TValue item)
  44.     {
  45.         return this.baseCollection.Any(i => this.selector(i).Equals(item));
  46.     }
  47.  
  48.     public void CopyTo(TValue[] array, int arrayIndex)
  49.     {
  50.         throw new NotSupportedException();
  51.     }
  52.  
  53.     public int Count
  54.     {
  55.         get { return this.baseCollection.Count; }
  56.     }
  57.  
  58.     public bool IsReadOnly
  59.     {
  60.         get { return true; }
  61.     }
  62.  
  63.     public bool Remove(TValue item)
  64.     {
  65.         throw new NotSupportedException();
  66.     }
  67.  
  68.     #endregion
  69.  
  70.     #region IEnumerable<T> Members
  71.  
  72.     public IEnumerator<TValue> GetEnumerator()
  73.     {
  74.         return this.baseCollection.Select(i => this.selector(i)).GetEnumerator();
  75.     }
  76.  
  77.     #endregion
  78.  
  79.     #region IEnumerable Members
  80.  
  81.     System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  82.     {
  83.         return this.baseCollection.Select(i => this.selector(i)).GetEnumerator();
  84.     }
  85.  
  86.     #endregion
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement