通过泛型+拓展函数实现。即,通过函数实现管道,使用拓展函数进行管道连接。
1 //管道函数 2 public static class Fs 3 { 4 //第一段管道 5 public static Func<string, int> F1 = (x) => int.Parse(x); 6 //第二段管道 7 public static Func<int, bool> F2 = (x) => x > 5; 8 } 9 10 //管道连接器 11 public static class PipelineLinker2 12 { 13 public static OUTPUT FlowTo<INPUT, OUTPUT>(this INPUT data, Func<INPUT, OUTPUT> process) 14 { 15 return process(data); 16 } 17 }
使用方法:
var v = "6".FlowTo(Fs.F1).FlowTo(Fs.F2);