欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# 如何實現(xiàn)一個帶通知的List<T>

 更新時間:2021年02月27日 11:58:47   作者:Hello——尋夢者!  
這篇文章主要介紹了C# 如何實現(xiàn)一個帶通知的List<T>,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下

背景

  在很多場景下面我們需要在集合發(fā)生變化的時候能夠通過一個事件對外進行通知,默認的List<T>并沒有此類功能,所以對于這一類需求的業(yè)務(wù)場景下我們需要自己進行相關(guān)的擴展,這樣才能夠符合我們這一需求,這里我來列舉一個在項目中經(jīng)常用到的一個擴展類,在后面我們會對這個進行具體的分析和使用到的C#知識點進行關(guān)注。

實現(xiàn)

  這里貼出具體的代碼實現(xiàn)

using System;
using System.Collections.Generic;
using System.Linq;

namespace XXX.XXX.Core.Utils
{
  public class ItemsChangedEventArgs<T> : EventArgs
  {
    public IList<T> RemovedItems { get; private set; }
    public IList<T> AddedItems { get; private set; }
    public ItemsChangedEventArgs(IList<T> removedItems, IList<T> addItems)
    {
      RemovedItems = removedItems;
      AddedItems = addItems;
    }
  }

  public delegate void ListItemsChangedEventHandler<T>(object sender, ItemsChangedEventArgs<T> args);

  public class NotifyList<T> : List<T>
  {
    public static NotifyList<T> Empty
    {
      get { return new NotifyList<T>(); }
    }

    public event ListItemsChangedEventHandler<T> ItemsChanged;
    protected void OnItemsChanged(IList<T> removedItems, IList<T> addedItems)
    {
      ListItemsChangedEventHandler<T> temp = ItemsChanged;
      temp?.Invoke(this, new ItemsChangedEventArgs<T>(removedItems, addedItems));
    }

    public new void Add(T item)
    {
      base.Add(item);

      OnItemsChanged(Empty, new List<T> { item });
    }

    public new void AddRange(IEnumerable<T> collection)
    {
      base.AddRange(collection);

      OnItemsChanged(Empty, collection.ToList());
    }

    public new void Clear()
    {
      T[] array = new T[this.Count];
      this.CopyTo(array);

      base.Clear();

      OnItemsChanged(array.ToList(), Empty);
    }

    public new bool Remove(T item)
    {
      bool ret = base.Remove(item);
      if (ret) OnItemsChanged(new List<T> { item }, Empty);
      return ret;
    }

    public new int RemoveAll(Predicate<T> match)
    {
      IList<T> removedItems = FindAll(match);

      int count = base.RemoveAll(match);
      if (removedItems.Count != count)
      {
        throw new Exception("[NotifyList][RemoveAll][The number of elements found by the predicate does not match the number of elements removed.]");
      }

      OnItemsChanged(removedItems, Empty);
      return count;
    }

    public new void RemoveAt(int index)
    {
      T removedItem = this[index];
      base.RemoveAt(index);
      OnItemsChanged(new List<T> { removedItem }, Empty);
    }

    public new void RemoveRange(int index, int count)
    {
      IEnumerable<T> range = this.Skip(index + 1).Take(count);
      base.RemoveRange(index, count);
      OnItemsChanged(range.ToList(), Empty);
    }
  }
}

 注意事項

  1 基類中Add這些方法都是非虛方法,這里不能使用重載,所以在自己實現(xiàn)的每一個方法中需要使用 new 關(guān)鍵字進行覆蓋。

       2  在具體使用的時候需要訂閱ItemsChanged事件。

以上就是C# 如何實現(xiàn)一個帶通知的List<T>的詳細內(nèi)容,更多關(guān)于C# 實現(xiàn)一個帶通知的List<T>的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論