/// <summary>
        /// Creates the silent event args list.
        /// </summary>
        /// <param name="suspensionContext">The suspension context.</param>
        /// <typeparam name="T">The type of collection item.</typeparam>
        /// <returns>The <see cref="ICollection{NotifyRangedCollectionChangedEventArgs}"/>.</returns>
        public static ICollection <NotifyRangedCollectionChangedEventArgs> CreateSilentEvents <T>(this SuspensionContext <T> suspensionContext)
        {
            Argument.IsNotNull(nameof(suspensionContext), suspensionContext);
            Argument.IsValid(nameof(suspensionContext.Mode), suspensionContext.Mode, mode => mode == SuspensionMode.Silent);

            return(ArrayShim.Empty <NotifyRangedCollectionChangedEventArgs>());
        }
        /// <summary>
        /// The create event args list.
        /// </summary>
        /// <param name="suspensionContext">The suspension context.</param>
        /// <typeparam name="T">The type of collection item.</typeparam>
        /// <returns>The <see cref="ICollection{NotifyRangedCollectionChangedEventArgs}"/>.</returns>
        public static ICollection <NotifyRangedCollectionChangedEventArgs> CreateEvents <T>(this SuspensionContext <T> suspensionContext)
        {
            // No suspension context is the same as None mode
            var mode = suspensionContext?.Mode ?? SuspensionMode.None;

            // Fast return for no items in not None modes
            if (mode != SuspensionMode.None && suspensionContext?.ChangedItems.Count == 0)
            {
                return(ArrayShim.Empty <NotifyRangedCollectionChangedEventArgs>());
            }

            if (!SuspensionContext <T> .EventsGeneratorsRegistry.Value.TryGetValue(mode, out var createEvents))
            {
                // ReSharper disable once LocalizableElement
                throw new ArgumentOutOfRangeException(nameof(mode), $"The suspension mode '{mode}' is unhandled.");
            }

            return(createEvents(suspensionContext));
        }
Beispiel #3
0
        /// <summary>
        /// Converts the collection to an array.
        /// </summary>
        /// <param name="collection">The collection.</param>
        /// <param name="elementType">Type of the element.</param>
        /// <returns>Array.</returns>
        public static Array ToArray(this IEnumerable collection, Type elementType)
        {
            Argument.IsNotNull("elementType", elementType);

            var internalList = new List <object>(collection != null ? collection.Cast <object>() : ArrayShim.Empty <object>());
            var array        = Array.CreateInstance(elementType, internalList.Count);

            var index = 0;

            foreach (var item in internalList)
            {
                array.SetValue(item, index++);
            }

            return(array);
        }