Gstc.Collections.ObservableLists icon indicating copy to clipboard operation
Gstc.Collections.ObservableLists copied to clipboard

AddRange throws an InvalidCastException if the seeded parameter isn't of type IList<T>

Open SabigGasim opened this issue 1 year ago • 2 comments

SabigGasim avatar Aug 10 '23 18:08 SabigGasim

As in the title, i found that when i seed the AddRange with an IEnumerable, i have to explicitly call .ToList() for it to not throw InvalidCastException. seems to be an easy fix as seen here:

        public void AddRange(IEnumerable<TItem> items)
        {
            using (_monitor.BlockReentrancy())
            {
            //this should be a condition where if the IEnumerable is of type (IList), then implicitly cast it. otherwise call .ToList()
                NotifyCollectionChangedEventArgs e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)items, _list.Count);
                this.CollectionChanging?.Invoke(this, e);
                this.Adding?.Invoke(this, e);
                AddRangeInternal(items);
                OnPropertyChangedCountAndIndex();
                if (IsAddRangeResetEvent)
                {
                    this.CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                }
                else
                {
                    this.CollectionChanged?.Invoke(this, e);
                }

                this.Added?.Invoke(this, e);
            }
        }

SabigGasim avatar Aug 10 '23 19:08 SabigGasim

Ah. Been out of the office. I will take a look at this. Your suggestion makes sense. I will also check to see if I can just cast it to IEnumerable. The MS NotifyCollectionChangedEventArgs may accept it. I'm not sure.

gsonnenf avatar Sep 01 '23 18:09 gsonnenf