コード例 #1
0
ファイル: GeneralList.cs プロジェクト: ARLM-Attic/clr-toolbox
        /// <inheriteddoc />
        public void ForEach <T, TState>(Action <IForEachItemExecutionContext <T, TState> > action, Func <T, long, TState> actionStateFactory, bool takeAll)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            if (actionStateFactory == null)
            {
                throw new ArgumentNullException("actionStateFactory");
            }

            IEnumerable <object> allItems = this;

            IEnumerable <object> filteredItems = CollectionHelper.Where(allItems,
                                                                        delegate(object item)
            {
                return(item is T || takeAll);
            });

            IEnumerable <T> castedItems = CollectionHelper.Select(filteredItems,
                                                                  delegate(object item)
            {
                return(GlobalConverter.Current
                       .ChangeType <T>(item));
            });

            using (IEnumerator <T> e = castedItems.GetEnumerator())
            {
                long index = -1;

                while (e.MoveNext())
                {
                    SimpleForEachItemExecutionContext <T, TState> ctx = new SimpleForEachItemExecutionContext <T, TState>();
                    ctx.Cancel = false;
                    ctx.Index  = ++index;
                    ctx.Item   = e.Current;
                    ctx.State  = actionStateFactory(ctx.Item, ctx.Index);

                    action(ctx);

                    if (ctx.Cancel)
                    {
                        // cancel whole operation
                        break;
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Extended foreach operation for a sequence.
        /// </summary>
        /// <typeparam name="T">Type of the items.</typeparam>
        /// <typeparam name="S">Type of the state object.</typeparam>
        /// <param name="seq">The sequence.</param>
        /// <param name="action">The action to invoke.</param>
        /// <param name="actionStateFactory">
        /// The function that returns the state object for argument of <paramref name="action" />.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="seq" />, <paramref name="action" /> and/or
        /// <paramref name="actionStateFactory" /> are <see langword="null" />.
        /// </exception>
        public static void ForEach <T, S>(IEnumerable <T> seq, Action <IForEachItemExecutionContext <T, S> > action, Func <T, long, S> actionStateFactory)
        {
            if (seq == null)
            {
                throw new ArgumentNullException("seq");
            }

            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            if (actionStateFactory == null)
            {
                throw new ArgumentNullException("actionStateFactory");
            }

            IGeneralList genList = seq as IGeneralList;

            if (genList != null)
            {
                genList.ForEach <T, S>(action, actionStateFactory);
                return;
            }

            using (IEnumerator <T> e = seq.GetEnumerator())
            {
                long index = -1;

                while (e.MoveNext())
                {
                    SimpleForEachItemExecutionContext <T, S> ctx = new SimpleForEachItemExecutionContext <T, S>();
                    ctx.Cancel = false;
                    ctx.Index  = ++index;
                    ctx.Item   = e.Current;
                    ctx.State  = actionStateFactory(ctx.Item, ctx.Index);

                    action(ctx);

                    if (ctx.Cancel)
                    {
                        // cancel whole operation
                        break;
                    }
                }
            }
        }