using System; using System.Collections.Generic; namespace ConsoleApp2 { class Program { static void Main(string[] args) { App app = new App(); app.Use(next => { Console.WriteLine(1); return i => { Console.WriteLine("1-before"); next.Invoke(11); Console.WriteLine("1-after"); }; }); app.Use(next => { Console.WriteLine(2); return i => { Console.WriteLine("2-before"); next.Invoke(22); Console.WriteLine("2-after"); }; }); app.Use(next => { Console.WriteLine(3); return i => { Console.WriteLine("3-before"); next.Invoke(33); Console.WriteLine("3-after"); }; }); Action<int> act= app.Builder(); act.Invoke(100); Console.ReadLine(); } } public class App { List<Func<Action<int>, Action<int>>> list = new List<Func<Action<int>, Action<int>>>(); public void Use(Func<Action<int>, Action<int>> func) { list.Add(func); } public Action<int> Builder() { //当集合为空时执行,相当于.net core 下的404 Action<int> action = act => { Console.WriteLine("404"); }; list.Reverse(); foreach (Func<Action<int>, Action<int>> item in list) { action = item.Invoke(action); } return action; } } }