private static void RemoveItemFromSourceCollection(IEnumerable <TItemSource> source, CollectionChangedArgs <TItemVM> args)
        {
            var listSource       = source as IList <TItemSource>;
            var collectionSource = source as ICollection <TItemSource>;

            if (listSource != null)
            {
                bool listSourceHasChanged = !Object.ReferenceEquals(listSource[args.Index], args.OldItem.Source);
                ThrowOutOfSyncExceptionIf(listSourceHasChanged);
                listSource.RemoveAt(args.Index);
            }
            else if (collectionSource != null)
            {
                bool itemWasFound         = collectionSource.Remove(args.OldItem.Source);
                bool listSourceHasChanged = collectionSource.Count != args.Collection.Count;

                ThrowOutOfSyncExceptionIf(!itemWasFound || listSourceHasChanged);
            }
        }
        private static void SetItemInSourceCollection(IEnumerable <TItemSource> source, CollectionChangedArgs <TItemVM> args)
        {
            var listSource       = source as IList <TItemSource>;
            var collectionSource = source as ICollection <TItemSource>;

            if (listSource != null)
            {
                bool listSourceHasChanged = !Object.Equals(listSource[args.Index], args.OldItem.Source);
                ThrowOutOfSyncExceptionIf(listSourceHasChanged);

                listSource[args.Index] = args.NewItem.Source;
            }
            else
            {
                if (collectionSource != null)
                {
                    bool itemWasFound = collectionSource.Remove(args.OldItem.Source);
                    ThrowOutOfSyncExceptionIf(!itemWasFound);

                    collectionSource.Add(args.NewItem.Source);
                }
            }

            if (collectionSource != null)
            {
                bool listSourceHasChanged = collectionSource.Count != args.Collection.Count;
                ThrowOutOfSyncExceptionIf(listSourceHasChanged);
            }
        }
        private static void InsertItemIntoSourceCollection(IEnumerable <TItemSource> source, CollectionChangedArgs <TItemVM> args)
        {
            var listSource       = source as IList <TItemSource>;
            var collectionSource = source as ICollection <TItemSource>;

            if (listSource != null)
            {
                listSource.Insert(args.Index, args.NewItem.Source);
            }
            else
            {
                if (collectionSource != null)
                {
                    collectionSource.Add(args.NewItem.Source);
                }
            }

            if (collectionSource != null)
            {
                bool listSourceHasChanged = collectionSource.Count != args.Collection.Count;
                ThrowOutOfSyncExceptionIf(listSourceHasChanged);
            }
        }