Skip to content

Commit beb2fa2

Browse files
authored
Merge pull request #6466 from smoogipoo/fix-bindablelist-cast
Fix `BindableList` methods crashing with non-`IList` collections
2 parents 17ad069 + 3ac37de commit beb2fa2

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

osu.Framework.Tests/Bindables/BindableListTest.cs

+22
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,17 @@ public void TestAddRangeBranchingBinds()
476476
Assert.That(b4.Count, Is.EqualTo(7));
477477
}
478478

479+
[Test]
480+
public void TestAddHashSetWithCallback()
481+
{
482+
var b1 = new BindableList<int>();
483+
484+
b1.CollectionChanged += (_, _) => { };
485+
b1.AddRange(new HashSet<int>([1, 2, 3, 4]));
486+
487+
Assert.That(b1.Count, Is.EqualTo(4));
488+
}
489+
479490
#endregion
480491

481492
#region .Move(item)
@@ -1193,6 +1204,17 @@ public void TestReplaceRangeNotifiesBoundLists()
11931204
Assert.That(triggeredArgs.OldStartingIndex, Is.EqualTo(0));
11941205
}
11951206

1207+
[Test]
1208+
public void TestReplaceRangeHashSetWithCallback()
1209+
{
1210+
var b1 = new BindableList<int>([1]);
1211+
1212+
b1.CollectionChanged += (_, _) => { };
1213+
b1.ReplaceRange(0, 1, new HashSet<int>([1, 2, 3, 4]));
1214+
1215+
Assert.That(b1.Count, Is.EqualTo(4));
1216+
}
1217+
11961218
#endregion
11971219

11981220
#region .Clear()

osu.Framework/Bindables/BindableList.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,9 @@ private int removeAll(Predicate<T> match, HashSet<BindableList<T>> appliedInstan
320320
/// <param name="count">The count of items to be removed.</param>
321321
/// <param name="newItems">The items to replace the removed items with.</param>
322322
public void ReplaceRange(int index, int count, IEnumerable<T> newItems)
323-
=> replaceRange(index, count, newItems as ICollection<T> ?? newItems.ToArray(), new HashSet<BindableList<T>>());
323+
=> replaceRange(index, count, newItems as IList ?? newItems.ToArray(), new HashSet<BindableList<T>>());
324324

325-
private void replaceRange(int index, int count, ICollection<T> newItems, HashSet<BindableList<T>> appliedInstances)
325+
private void replaceRange(int index, int count, IList newItems, HashSet<BindableList<T>> appliedInstances)
326326
{
327327
if (checkAlreadyApplied(appliedInstances)) return;
328328

@@ -335,7 +335,7 @@ private void replaceRange(int index, int count, ICollection<T> newItems, HashSet
335335
List<T> removedItems = CollectionChanged == null ? null : collection.GetRange(index, count);
336336

337337
collection.RemoveRange(index, count);
338-
collection.InsertRange(index, newItems);
338+
collection.InsertRange(index, (IEnumerable<T>)newItems);
339339

340340
if (bindings != null)
341341
{
@@ -347,7 +347,7 @@ private void replaceRange(int index, int count, ICollection<T> newItems, HashSet
347347
}
348348
}
349349

350-
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, (IList)newItems, removedItems!, index));
350+
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItems, removedItems!, index));
351351
}
352352

353353
/// <summary>
@@ -544,23 +544,23 @@ public virtual void UnbindFrom(IUnbindable them)
544544
/// <param name="items">The collection whose items should be added to this collection.</param>
545545
/// <exception cref="InvalidOperationException">Thrown if this collection is <see cref="Disabled"/></exception>
546546
public void AddRange(IEnumerable<T> items)
547-
=> addRange(items as ICollection<T> ?? items.ToArray(), new HashSet<BindableList<T>>());
547+
=> addRange(items as IList ?? items.ToArray(), new HashSet<BindableList<T>>());
548548

549-
private void addRange(ICollection<T> items, HashSet<BindableList<T>> appliedInstances)
549+
private void addRange(IList items, HashSet<BindableList<T>> appliedInstances)
550550
{
551551
if (checkAlreadyApplied(appliedInstances)) return;
552552

553553
ensureMutationAllowed();
554554

555-
collection.AddRange(items);
555+
collection.AddRange((IEnumerable<T>)items);
556556

557557
if (bindings != null)
558558
{
559559
foreach (var b in bindings)
560560
b.addRange(items, appliedInstances);
561561
}
562562

563-
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)items, collection.Count - items.Count));
563+
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, items, collection.Count - items.Count));
564564
}
565565

566566
/// <summary>

0 commit comments

Comments
 (0)