Gstc.Collections.ObservableLists
Gstc.Collections.ObservableLists copied to clipboard
AddRange throws an InvalidCastException if the seeded parameter isn't of type IList<T>
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);
}
}
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.