public static SynchronizationContextCollection <TResult> ToSyncedSynchronizationContextCollection <TSource, TResult>(
            this ISynchronizableNotifyChangedCollection <TSource> source,
            Func <TSource, TResult> converter,
            SynchronizationContext context)
        {
            lock (source.Synchronizer.LockObject)
            {
                var result = new SynchronizationContextCollection <TResult>(source.Select(converter), context);

                var collectionChangedListener = SynchronizableNotifyChangedCollectionHelper.CreateSynchronizableCollectionChangedEventListener(source, result,
                                                                                                                                               converter);
                result.Synchronizer.EventListeners.Add(collectionChangedListener);
                return(result);
            }
        }
Exemple #2
0
        public static SortedObservableCollection <TSource, TKey> ToSyncedSortedObservableCollection <TSource, TKey>(
            this ISynchronizableNotifyChangedCollection <TSource> source,
            Func <TSource, TKey> keySelector,
            IComparer <TKey> comparer = null,
            bool isDescending         = false)
        {
            var isDisposableType = typeof(IDisposable).GetTypeInfo().IsAssignableFrom(typeof(TSource).GetTypeInfo());

            lock (source.Synchronizer.LockObject)
            {
                var result = new SortedObservableCollection <TSource, TKey>(keySelector, comparer, isDescending);

                foreach (var item in source)
                {
                    result.Add(item);
                }

                var collectionChangedListener = SynchronizableNotifyChangedCollectionHelper.CreateSynchronizableCollectionChangedEventListener(source, result,
                                                                                                                                               _ => _,
                                                                                                                                               removeAction: e =>
                {
                    var removeSourceItem = (TSource)e.OldItems[0];
                    result.RemoveAt(result.IndexOf(removeSourceItem));
                    if (isDisposableType)
                    {
                        ((IDisposable)removeSourceItem).Dispose();
                    }
                },
                                                                                                                                               moveAction: e => { },
                                                                                                                                               replaceAction: e =>
                {
                    var removeSourceItem = (TSource)e.OldItems[0];
                    result.RemoveAt(result.IndexOf(removeSourceItem));
                    result.Add((TSource)e.NewItems[0]);
                    if (isDisposableType)
                    {
                        ((IDisposable)removeSourceItem).Dispose();
                    }
                }
                                                                                                                                               );
                result.Synchronizer.EventListeners.Add(collectionChangedListener);
                return(result);
            }
        }
        public static ObservableSynchronizedCollection <TResult> ToSyncedObservableSynchronizedCollection <TSource, TResult>(
            this ISynchronizableNotifyChangedCollection <TSource> source,
            Func <TSource, TResult> converter)
        {
            lock (source.Synchronizer.LockObject)
            {
                var result = new ObservableSynchronizedCollection <TResult>();
                foreach (var item in source)
                {
                    result.Add(converter(item));
                }

                var collectionChangedListener =
                    SynchronizableNotifyChangedCollectionHelper.CreateSynchronizableCollectionChangedEventListener(
                        source, result,
                        converter);
                result.Synchronizer.EventListeners.Add(collectionChangedListener);
                return(result);
            }
        }