Filter管道

pojianbing發表於2024-08-31

using Cronos;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var filters = new List<Filter> {
                new Filter("A"),
                new Filter("B"),
            };

            var fn = filters.GetEnumerator();
            InvokeNext(ref fn);

            Console.WriteLine("All filters have been invoked.");
        }

        static void InvokeNext(ref List<Filter>.Enumerator filters)
        {
            if (filters.MoveNext())
            {
                InvokeCurrent(ref filters);
                return;
            }

            Console.WriteLine("調起真正的執行方法.");
        }

        static void InvokeCurrent(ref List<Filter>.Enumerator filters)
        {
            var current = filters.Current;

            current.Before();

            InvokeNext(ref filters);

            current.After();
        }
    }

    class Filter
    {
        private string name;

        public Filter(string name)
        {
            this.name = name;
        }

        public void Before()
        {
            Console.WriteLine("Before filter " + name);
        }

        public void Current()
        {
            Console.WriteLine("Current filter " + name);
        }

        public void After()
        {
            Console.WriteLine("After filter " + name);
        }
    }
}

輸出:
Before filter A
Before filter B
調起真正的執行方法.
After filter B
After filter A
All filters have been invoked.