How to handle add to list event?

后端 未结 10 2330
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 22:55

I have a list like this:

List list = new List

How to handle adding new position to this list?

When

相关标签:
10条回答
  • 2020-11-29 23:26

    You could inherit from List and add your own handler, something like

    using System;
    using System.Collections.Generic;
    
    namespace test
    {
        class Program
        {
    
            class MyList<T> : List<T>
            {
    
                public event EventHandler OnAdd;
    
                public new void Add(T item) // "new" to avoid compiler-warnings, because we're hiding a method from base-class
                {
                    if (null != OnAdd)
                    {
                        OnAdd(this, null);
                    }
                    base.Add(item);
                }
            }
    
            static void Main(string[] args)
            {
                MyList<int> l = new MyList<int>();
                l.OnAdd += new EventHandler(l_OnAdd);
                l.Add(1);
            }
    
            static void l_OnAdd(object sender, EventArgs e)
            {
                Console.WriteLine("Element added...");
            }
        }
    }
    

    Warning

    1. Be aware that you have to re-implement all methods which add objects to your list. AddRange() will not fire this event, in this implementation.

    2. We did not overload the method. We hid the original one. If you Add() an object while this class is boxed in List<T>, the event will not be fired!

    MyList<int> l = new MyList<int>();
    l.OnAdd += new EventHandler(l_OnAdd);
    l.Add(1); // Will work
    
    List<int> baseList = l;
    baseList.Add(2); // Will NOT work!!!
    
    0 讨论(0)
  • 2020-11-29 23:27

    To piggy-back off Ahmad's use of Extension Methods, you can create your own class where the list is private with a public get method and a public add method.

    public class MyList
    {
        private List<SomeClass> PrivateSomeClassList;
        public List<SomeClass> SomeClassList
        {
            get
            {
                return PrivateSomeClassList;
            }
        }
    
        public void Add(SomeClass obj)
        {
            // do whatever you want
            PrivateSomeClassList.Add(obj);
        }
    }
    

    However, this class only provides access to List<> methods that you manually expose...hence may not be useful in cases where you need a lot of that functionality.

    0 讨论(0)
  • 2020-11-29 23:29

    One simple solution is to introduce an Add method for the list in your project and handle the event there. It doesn't answer the need for an event handler but can be useful for some small projects.

    AddToList(item) // or
    AddTo(list,item)
    ////////////////////////
    
    void AddTo(list,item)
    {
        list.Add(item);
        // event handling
    }
    

    instead of

    list.Add(item);
    
    0 讨论(0)
  • 2020-11-29 23:33

    No need for adding an event just add the method.

    public class mylist:List<string>
    {
      public void Add(string p)
      {
         // Do cool stuff here
         base.Add(p);
      }
    }
    
    0 讨论(0)
  • 2020-11-29 23:34

    //List overrider class

    public class ListWithEvents<T> : List<T>
        {
    
            public delegate void AfterChangeHandler();
    
            public AfterChangeHandler OnChangeEvent;        
    
            public Boolean HasAddedItems = false;
            public Boolean HasRemovedItems = false;
    
            public bool HasChanges
            {
                get => HasAddedItems || HasRemovedItems;
                set
                {                
                    HasAddedItems = value;
                    HasRemovedItems = value; 
                }
            }
    
            public new void Add(T item)
            {
                base.Add(item);
                HasAddedItems = true;
                if(OnChangeEvent != null) OnChangeEvent();
                OnChange();
            }
    
            public new void AddRange(IEnumerable<T> collection)
            {
                base.AddRange(collection);
                HasAddedItems = true;
                if (OnChangeEvent != null) OnChangeEvent();
                OnChange();
            }
    
            public new void Insert(int index,T item)
            {
                base.Insert(index, item);
                HasAddedItems = true;
                if (OnChangeEvent != null) OnChangeEvent();
                OnChange();
            }
    
            public new void InsertRange(int index, IEnumerable<T> collection)
            {
                base.InsertRange(index, collection);
                HasAddedItems = true;
                if (OnChangeEvent != null) OnChangeEvent();
                OnChange();
            }
    
    
            public new void Remove(T item)
            {
                base.Remove(item);
                HasRemovedItems = true;
                if (OnChangeEvent != null) OnChangeEvent();
                OnChange();
            }
    
            public new void RemoveAt(int index)
            {
                HasRemovedItems = true;
                if (OnChangeEvent != null) OnChangeEvent();
                OnChange();
            }
    
            public new void RemoveRange(int index,int count)
            {
                HasRemovedItems = true;
                if (OnChangeEvent != null) OnChangeEvent();
                OnChange();
            }
    
            public new void Clear()
            {
                base.Clear();
                HasRemovedItems = true;
                if (OnChangeEvent != null) OnChangeEvent();
                OnChange();
            }
    
            public virtual void OnChange()
            {
    
            }
      
        }
    
    0 讨论(0)
  • 2020-11-29 23:36

    You can't do this with List<T>. However, you can do it with ObservableCollection<T>. See ObservableCollection<T> Class.

    0 讨论(0)
提交回复
热议问题