//递归 public class LinC { public static string Go() { Func<Func<int, int>, Func<int, int>> fun = child => x => x == 0 ? 1 : x * child(x - 1); var F = Recursion<int>.GetRecursion(fun); string aa = F(5).ToString(); return aa; } public static void test() { List<FileInfo> res = new List<FileInfo>(); DirectoryInfo dir = new DirectoryInfo(@"E:\AAA"); //传入参数起作用,返回参数无实际意义,只为补完整函数体 Func<Func<DirectoryInfo, DirectoryInfo>, Func<DirectoryInfo, DirectoryInfo>> fun = child => x => { res.AddRange(x.GetFiles()); foreach (var read in x.GetDirectories()) child(read); //起终止递归的作用 return dir; }; var F = Recursion<DirectoryInfo>.GetRecursion(fun); var ddd = F(dir); } } public class Recursion<K> { //自身代理 delegate T LinDelegate<T>(LinDelegate<T> delageteSelf); public static Func<Func<Func<K, K>, Func<K, K>>, Func<K, K>> GetRecursionBase() { LinDelegate<Func<Func<Func<K, K>, Func<K, K>>, Func<K, K>>> self = y => f => x => f(y(y)(f))(x); Func< Func< Func<K, K>, Func<K, K> >, Func<K, K> > res = self(self); return res; } public static Func<K, K> GetRecursion(Func<Func<K, K>, Func<K, K>> fun) { var F = Recursion<K>.GetRecursionBase(); Func<K, K> res = F(fun); return res; } }