From bf4b07b2acdbe6edb73e8d884004294260ea4109 Mon Sep 17 00:00:00 2001 From: Andrew Hoefling Date: Sun, 10 Mar 2019 22:25:55 -0400 Subject: [PATCH] Added new OnCollectionChanged overloads to reduce need for additional if checks. --- .../ObjectModel/ObservableCollection.cs | 36 ++++++++++--------- .../ObservableCollection_MethodsTest.cs | 2 +- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs b/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs index 0ea90f49e780..ea7768af7282 100644 --- a/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs +++ b/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs @@ -218,7 +218,7 @@ protected override void ReplaceItemsRange(int index, int count, IEnumerable c OnCountPropertyChanged(); OnIndexerPropertyChanged(); - OnCollectionChanged(NotifyCollectionChangedAction.Replace, itemsToReplace, collection, index); + OnCollectionChanged(NotifyCollectionChangedAction.Replace, itemsToReplace, (IList)collection, index); } @@ -397,14 +397,15 @@ protected void CheckReentrancy() /// private void OnCollectionChanged(NotifyCollectionChangedAction action, object item, int index) { - if (item is IList) - { - OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, (IList)item, index)); - } - else - { - OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item, index)); - } + OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item, index)); + } + + /// + /// Helper to raise CollectionChanged event to any listeners + /// + private void OnCollectionChanged(NotifyCollectionChangedAction action, IList items, int index) + { + OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, items, index)); } /// @@ -420,14 +421,15 @@ private void OnCollectionChanged(NotifyCollectionChangedAction action, object it /// private void OnCollectionChanged(NotifyCollectionChangedAction action, object oldItem, object newItem, int index) { - if (oldItem is IList && newItem is IList) - { - OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, (IList)oldItem, (IList)newItem, index)); - } - else - { - OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, newItem, oldItem, index)); - } + OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, newItem, oldItem, index)); + } + + /// + /// Helper to raise CollectionChanged event to any listeners + /// + private void OnCollectionChanged(NotifyCollectionChangedAction action, IList oldItems, IList newItems, int index) + { + OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, newItems, oldItems, index)); } /// diff --git a/src/System.ObjectModel/tests/ObservableCollection/ObservableCollection_MethodsTest.cs b/src/System.ObjectModel/tests/ObservableCollection/ObservableCollection_MethodsTest.cs index 6f0a180315ac..504f899fa070 100644 --- a/src/System.ObjectModel/tests/ObservableCollection/ObservableCollection_MethodsTest.cs +++ b/src/System.ObjectModel/tests/ObservableCollection/ObservableCollection_MethodsTest.cs @@ -690,7 +690,7 @@ public static void RemoveRange_NotifyCollectionChanged_EventArgs_IndexOfZero_Tes } [Fact] - public static void RemoveRange_NotifyCollectionChanged_EventArgs_IndexMiddle_Test() + public static void RemoveRange_NotifyCollectionChanged_EventArgs_IndexMiddle_Test() { int[] initialData = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; int[] actualDataRemoved = new int[0];