Beispiel #1
0
        /// <summary>
        /// Inserts items from the specified fromList collection
        /// at the specified location in the toList collection.
        /// </summary>
        /// <param name="toList"></param>
        /// <param name="fromList"></param>
        /// <param name="startIndex"></param>
        public static void InsertRange(
            this IList toList, IEnumerable fromList, int startIndex)
        {
            AssertArg.IsNotNull(toList, nameof(toList));
            AssertArg.IsNotNull(fromList, nameof(fromList));
            AssertArg.IsGreaterThanOrEqual(0, startIndex, nameof(startIndex));

            IRangeOperations collection = toList as IRangeOperations;

            if (collection != null)
            {
                collection.InsertRange(fromList, startIndex);
                return;
            }

            var  suspendableList = toList as ISuspendChangeNotification;
            bool wasSuspended    = false;

            try
            {
                if (suspendableList != null)
                {
                    wasSuspended = suspendableList.ChangeNotificationSuspended;
                    suspendableList.ChangeNotificationSuspended = true;
                }

                int newIndex = startIndex;

                foreach (var item in fromList)
                {
                    toList.Insert(newIndex, item);
                    newIndex++;
                }
            }
            finally
            {
                if (suspendableList != null && !wasSuspended)
                {
                    try
                    {
                        suspendableList.ChangeNotificationSuspended = false;
                    }
                    catch (Exception)
                    {
                        /* Suppress. */
                    }
                }
            }
        }