Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SabigGasim opened this issue Aug 10, 2023 · 2 comments

Comments

@SabigGasim
Copy link

No description provided.

@SabigGasim
Copy link
Author

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 SabigGasim changed the title AddRange throws an InvalidCasaat AddRange throws an InvalidCastException if the seeded parameter isn't of type IList<T> Aug 10, 2023
@gsonnenf
Copy link
Owner

gsonnenf commented Sep 1, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants