public MergeableCollection(IEnumerable <TItem> dataObjects, bool sort) { SyncRoot = new object(); if (!sort) { _itemComparer.StopComparisons(); } // We don't really want to constrain based on the type being IMergeable or comparable // This is a very specific check. We want to ensure that this type supports IMergeable<T>, not IMergeable<SomethingElse> _areItemsMergable = Utility.IsInterfaceImplemented(typeof(TItem), typeof(IMergeable <TKey, TItem>)); _areItemsNotifiable = Utility.IsInterfaceImplemented(typeof(TItem), typeof(INotifyPropertyChanged)); if (dataObjects == null) { _items = new ObservableCollection <TItem>(); } else { _items = new ObservableCollection <TItem>(_itemComparer.OrderedList(dataObjects)); if (_areItemsNotifiable) { lock (SyncRoot) { foreach (INotifyPropertyChanged item in _items) { item.PropertyChanged += _OnItemChanged; } } } } if (_areItemsMergable) { _fkidLookup = new Dictionary <TKey, TItem>(); foreach (IMergeable <TKey, TItem> item in _items) { //Assert.IsNotNull(item.FKID); _fkidLookup.Add(item.FKID, (TItem)item); } } }
public _Comparer() { if (Utility.IsInterfaceImplemented(typeof(TItem), typeof(IComparable <TItem>))) { _defaultComparison = (left, right) => { if (object.ReferenceEquals(left, right)) { return(0); } var comparableLeft = (IComparable <TItem>)left; if (comparableLeft == null) { return(-1); } return(comparableLeft.CompareTo(right)); }; } _trueComparison = _defaultComparison; }