├── .gitignore ├── LICENSE ├── README.md └── src ├── PatternMatchingExtension.Test ├── App.config ├── PatternMatchingExtension.Test.csproj ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── PatternMatchingExtension.sln └── PatternMatchingExtension ├── Cps.cs ├── Function.cs ├── MatchExpression.cs ├── PatternMatchingExtension.csproj ├── Properties └── AssemblyInfo.cs └── _.cs /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | 24 | # Visual Studio 2015 cache/options directory 25 | .vs/ 26 | # Uncomment if you have tasks that create the project's static files in wwwroot 27 | #wwwroot/ 28 | 29 | # MSTest test Results 30 | [Tt]est[Rr]esult*/ 31 | [Bb]uild[Ll]og.* 32 | 33 | # NUNIT 34 | *.VisualState.xml 35 | TestResult.xml 36 | 37 | # Build Results of an ATL Project 38 | [Dd]ebugPS/ 39 | [Rr]eleasePS/ 40 | dlldata.c 41 | 42 | # DNX 43 | project.lock.json 44 | artifacts/ 45 | 46 | *_i.c 47 | *_p.c 48 | *_i.h 49 | *.ilk 50 | *.meta 51 | *.obj 52 | *.pch 53 | *.pdb 54 | *.pgc 55 | *.pgd 56 | *.rsp 57 | *.sbr 58 | *.tlb 59 | *.tli 60 | *.tlh 61 | *.tmp 62 | *.tmp_proj 63 | *.log 64 | *.vspscc 65 | *.vssscc 66 | .builds 67 | *.pidb 68 | *.svclog 69 | *.scc 70 | 71 | # Chutzpah Test files 72 | _Chutzpah* 73 | 74 | # Visual C++ cache files 75 | ipch/ 76 | *.aps 77 | *.ncb 78 | *.opendb 79 | *.opensdf 80 | *.sdf 81 | *.cachefile 82 | 83 | # Visual Studio profiler 84 | *.psess 85 | *.vsp 86 | *.vspx 87 | *.sap 88 | 89 | # TFS 2012 Local Workspace 90 | $tf/ 91 | 92 | # Guidance Automation Toolkit 93 | *.gpState 94 | 95 | # ReSharper is a .NET coding add-in 96 | _ReSharper*/ 97 | *.[Rr]e[Ss]harper 98 | *.DotSettings.user 99 | 100 | # JustCode is a .NET coding add-in 101 | .JustCode 102 | 103 | # TeamCity is a build add-in 104 | _TeamCity* 105 | 106 | # DotCover is a Code Coverage Tool 107 | *.dotCover 108 | 109 | # NCrunch 110 | _NCrunch_* 111 | .*crunch*.local.xml 112 | nCrunchTemp_* 113 | 114 | # MightyMoose 115 | *.mm.* 116 | AutoTest.Net/ 117 | 118 | # Web workbench (sass) 119 | .sass-cache/ 120 | 121 | # Installshield output folder 122 | [Ee]xpress/ 123 | 124 | # DocProject is a documentation generator add-in 125 | DocProject/buildhelp/ 126 | DocProject/Help/*.HxT 127 | DocProject/Help/*.HxC 128 | DocProject/Help/*.hhc 129 | DocProject/Help/*.hhk 130 | DocProject/Help/*.hhp 131 | DocProject/Help/Html2 132 | DocProject/Help/html 133 | 134 | # Click-Once directory 135 | publish/ 136 | 137 | # Publish Web Output 138 | *.[Pp]ublish.xml 139 | *.azurePubxml 140 | # TODO: Comment the next line if you want to checkin your web deploy settings 141 | # but database connection strings (with potential passwords) will be unencrypted 142 | *.pubxml 143 | *.publishproj 144 | 145 | # NuGet Packages 146 | *.nupkg 147 | # The packages folder can be ignored because of Package Restore 148 | **/packages/* 149 | # except build/, which is used as an MSBuild target. 150 | !**/packages/build/ 151 | # Uncomment if necessary however generally it will be regenerated when needed 152 | #!**/packages/repositories.config 153 | # NuGet v3's project.json files produces more ignoreable files 154 | *.nuget.props 155 | *.nuget.targets 156 | 157 | # Microsoft Azure Build Output 158 | csx/ 159 | *.build.csdef 160 | 161 | # Microsoft Azure Emulator 162 | ecf/ 163 | rcf/ 164 | 165 | # Microsoft Azure ApplicationInsights config file 166 | ApplicationInsights.config 167 | 168 | # Windows Store app package directory 169 | AppPackages/ 170 | BundleArtifacts/ 171 | 172 | # Visual Studio cache files 173 | # files ending in .cache can be ignored 174 | *.[Cc]ache 175 | # but keep track of directories ending in .cache 176 | !*.[Cc]ache/ 177 | 178 | # Others 179 | ClientBin/ 180 | ~$* 181 | *~ 182 | *.dbmdl 183 | *.dbproj.schemaview 184 | *.pfx 185 | *.publishsettings 186 | node_modules/ 187 | orleans.codegen.cs 188 | 189 | # RIA/Silverlight projects 190 | Generated_Code/ 191 | 192 | # Backup & report files from converting an old project file 193 | # to a newer Visual Studio version. Backup files are not needed, 194 | # because we have git ;-) 195 | _UpgradeReport_Files/ 196 | Backup*/ 197 | UpgradeLog*.XML 198 | UpgradeLog*.htm 199 | 200 | # SQL Server files 201 | *.mdf 202 | *.ldf 203 | 204 | # Business Intelligence projects 205 | *.rdl.data 206 | *.bim.layout 207 | *.bim_*.settings 208 | 209 | # Microsoft Fakes 210 | FakesAssemblies/ 211 | 212 | # GhostDoc plugin setting file 213 | *.GhostDoc.xml 214 | 215 | # Node.js Tools for Visual Studio 216 | .ntvs_analysis.dat 217 | 218 | # Visual Studio 6 build log 219 | *.plg 220 | 221 | # Visual Studio 6 workspace options file 222 | *.opt 223 | 224 | # Visual Studio LightSwitch build output 225 | **/*.HTMLClient/GeneratedArtifacts 226 | **/*.DesktopClient/GeneratedArtifacts 227 | **/*.DesktopClient/ModelManifest.xml 228 | **/*.Server/GeneratedArtifacts 229 | **/*.Server/ModelManifest.xml 230 | _Pvt_Extensions 231 | 232 | # Paket dependency manager 233 | .paket/paket.exe 234 | 235 | # FAKE - F# Make 236 | .fake/ 237 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yang Fan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PatternMatchingExtension 2 | 3 | Pattern matching extension methods for C#6 4 | 5 | # Example 6 | 7 | ```CSharp 8 | "foo" 9 | .Match(x => Console.WriteLine($"int {x}")) 10 | .Match(x => Console.WriteLine($"string {x}")) 11 | .Match<_>(x => Console.WriteLine($"object {x}")) 12 | ; 13 | 14 | new { a = 1, b = 2 } 15 | .Match((x1, x2) => Console.WriteLine($" {x1} {x2}")) 16 | .Match((x1, x2) => Console.WriteLine($" {x1} {x2}")) 17 | ; 18 | 19 | var len = new Function, int>() 20 | .Match(list => list.IsEmpty(), self => list => 0) 21 | .Match(list => true, self => list => list.Match((x, xs) => self.Invoke(xs) + 1)) 22 | .ToFunc() 23 | ; 24 | len(new List { 1, 2, 3, 4, 5 }).Println(); 25 | 26 | var fib = new Function() 27 | .Match(x => x == 0, self => x => 0) 28 | .Match(x => x == 1, self => x => 1) 29 | .Match(x => x > 1, self => x => self.Invoke(x - 1) + self.Invoke(x - 2)) 30 | .Match(_ => true, self => _ => { throw new ArgumentOutOfRangeException(nameof(_)); }) 31 | .ToFunc() 32 | ; 33 | $"fib(10) = {fib(10)}".Println(); 34 | ``` 35 | -------------------------------------------------------------------------------- /src/PatternMatchingExtension.Test/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/PatternMatchingExtension.Test/PatternMatchingExtension.Test.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {88689D16-D629-4270-8984-9797408C4C76} 8 | Exe 9 | Properties 10 | PatternMatchingExtension.Test 11 | PatternMatchingExtension.Test 12 | v4.6.1 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | {c8fc75a1-a78c-4a8d-b810-bb124eaf373c} 55 | PatternMatchingExtension 56 | 57 | 58 | 59 | 66 | -------------------------------------------------------------------------------- /src/PatternMatchingExtension.Test/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace PatternMatchingExtension.Test 5 | { 6 | static class Program 7 | { 8 | static void Main(string[] args) { 9 | "MatchExpression Test".Println(); 10 | 11 | "foo" 12 | .Match(x => Console.WriteLine($"int {x}")) 13 | .Match(x => Console.WriteLine($"string {x}")) 14 | .Match<_>(x => Console.WriteLine($"object {x}")) 15 | ; 16 | 17 | new { a = 1, b = 2 } 18 | .Match((x1, x2) => Console.WriteLine($" {x1} {x2}")) 19 | .Match((x1, x2) => Console.WriteLine($" {x1} {x2}")) 20 | ; 21 | 22 | var len = new Function, int>() 23 | .Match(list => list.IsEmpty(), self => list => 0) 24 | .Match(list => true, self => list => list.Match((x, xs) => self.Invoke(xs) + 1)) 25 | .ToFunc() 26 | ; 27 | len(new List { 1, 2, 3, 4, 5 }).Println(); 28 | 29 | var fib = new Function() 30 | .Match(x => x == 0, self => x => 0) 31 | .Match(x => x == 1, self => x => 1) 32 | .Match(x => x > 1, self => x => self.Invoke(x - 1) + self.Invoke(x - 2)) 33 | .Match(_ => true, self => _ => { throw new ArgumentOutOfRangeException(nameof(_)); }) 34 | .ToFunc() 35 | ; 36 | $"fib(10) = {fib(10)}".Println(); 37 | 38 | Console.ReadKey(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/PatternMatchingExtension.Test/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("PatternMatchingExtension.Test")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("PatternMatchingExtension.Test")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | //将 ComVisible 设置为 false 将使此程序集中的类型 18 | //对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("88689d16-d629-4270-8984-9797408c4c76")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, 33 | // 方法是按如下所示使用“*”: : 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/PatternMatchingExtension.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25123.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PatternMatchingExtension", "PatternMatchingExtension\PatternMatchingExtension.csproj", "{C8FC75A1-A78C-4A8D-B810-BB124EAF373C}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PatternMatchingExtension.Test", "PatternMatchingExtension.Test\PatternMatchingExtension.Test.csproj", "{88689D16-D629-4270-8984-9797408C4C76}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {C8FC75A1-A78C-4A8D-B810-BB124EAF373C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {C8FC75A1-A78C-4A8D-B810-BB124EAF373C}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {C8FC75A1-A78C-4A8D-B810-BB124EAF373C}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {C8FC75A1-A78C-4A8D-B810-BB124EAF373C}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {88689D16-D629-4270-8984-9797408C4C76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {88689D16-D629-4270-8984-9797408C4C76}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {88689D16-D629-4270-8984-9797408C4C76}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {88689D16-D629-4270-8984-9797408C4C76}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /src/PatternMatchingExtension/Cps.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace PatternMatchingExtension 5 | { 6 | public static class Cps 7 | { 8 | public static void Println(this object target) { 9 | target.Invoke(Console.WriteLine); 10 | } 11 | 12 | public static void Invoke(this object target, Action action) { 13 | action(target); 14 | } 15 | 16 | public static void ForEach(this IEnumerable enumerable, Action action) { 17 | foreach (var value in enumerable) { 18 | action(value); 19 | } 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/PatternMatchingExtension/Function.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | 5 | namespace PatternMatchingExtension 6 | { 7 | public class Function 8 | { 9 | private readonly List, Func>> _funcs = new List, Func>>(); 10 | private readonly List> _predicates = new List>(); 11 | 12 | public TResult Invoke(TArg1 arg1) { 13 | Debug.Assert(_predicates.Count == _funcs.Count); 14 | for (var i = 0; i < _funcs.Count; i++) { 15 | if (_predicates[i](arg1)) { 16 | return _funcs[i](this)(arg1); 17 | } 18 | } 19 | throw new NotSupportedException(_predicates.Count == 1 ? "Pattern is not comprehensive" : "Patterns are not comprehensive"); 20 | } 21 | 22 | public Function Match(Func predicate, Func, Func> func) { 23 | _predicates.Add(predicate); 24 | _funcs.Add(func); 25 | return this; 26 | } 27 | 28 | public Func ToFunc() { 29 | return Invoke; 30 | } 31 | } 32 | 33 | public class Function 34 | { 35 | private readonly List, Func>> _funcs = new List, Func>>(); 36 | private readonly List> _predicates = new List>(); 37 | 38 | public TResult Invoke(TArg1 arg1, TArg2 arg2) { 39 | Debug.Assert(_predicates.Count == _funcs.Count); 40 | for (var i = 0; i < _funcs.Count; i++) { 41 | if (_predicates[i](arg1, arg2)) { 42 | return _funcs[i](this)(arg1, arg2); 43 | } 44 | } 45 | throw new NotSupportedException(_predicates.Count == 1 ? "Pattern is not comprehensive" : "Patterns are not comprehensive"); 46 | } 47 | 48 | public Function Match(Func predicate, Func, Func> func) { 49 | _predicates.Add(predicate); 50 | _funcs.Add(func); 51 | return this; 52 | } 53 | 54 | public Func ToFunc() { 55 | return Invoke; 56 | } 57 | } 58 | 59 | public class Function 60 | { 61 | private readonly List, Func>> _funcs = new List, Func>>(); 62 | private readonly List> _predicates = new List>(); 63 | 64 | public TResult Invoke(TArg1 arg1, TArg2 arg2, TArg3 arg3) { 65 | Debug.Assert(_predicates.Count == _funcs.Count); 66 | for (var i = 0; i < _funcs.Count; i++) { 67 | if (_predicates[i](arg1, arg2, arg3)) { 68 | return _funcs[i](this)(arg1, arg2, arg3); 69 | } 70 | } 71 | throw new NotSupportedException(_predicates.Count == 1 ? "Pattern is not comprehensive" : "Patterns are not comprehensive"); 72 | } 73 | 74 | public Function Match(Func predicate, Func, Func> func) { 75 | _predicates.Add(predicate); 76 | _funcs.Add(func); 77 | return this; 78 | } 79 | 80 | public Func ToFunc() { 81 | return Invoke; 82 | } 83 | } 84 | 85 | public class Function 86 | { 87 | private readonly List, Func>> _funcs = new List, Func>>(); 88 | private readonly List> _predicates = new List>(); 89 | 90 | public TResult Invoke(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4) { 91 | Debug.Assert(_predicates.Count == _funcs.Count); 92 | for (var i = 0; i < _funcs.Count; i++) { 93 | if (_predicates[i](arg1, arg2, arg3, arg4)) { 94 | return _funcs[i](this)(arg1, arg2, arg3, arg4); 95 | } 96 | } 97 | throw new NotSupportedException(_predicates.Count == 1 ? "Pattern is not comprehensive" : "Patterns are not comprehensive"); 98 | } 99 | 100 | public Function Match(Func predicate, Func, Func> func) { 101 | _predicates.Add(predicate); 102 | _funcs.Add(func); 103 | return this; 104 | } 105 | 106 | public Func ToFunc() { 107 | return Invoke; 108 | } 109 | } 110 | 111 | public class Function 112 | { 113 | private readonly List, Func>> _funcs = new List, Func>>(); 114 | private readonly List> _predicates = new List>(); 115 | 116 | public TResult Invoke(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5) { 117 | Debug.Assert(_predicates.Count == _funcs.Count); 118 | for (var i = 0; i < _funcs.Count; i++) { 119 | if (_predicates[i](arg1, arg2, arg3, arg4, arg5)) { 120 | return _funcs[i](this)(arg1, arg2, arg3, arg4, arg5); 121 | } 122 | } 123 | throw new NotSupportedException(_predicates.Count == 1 ? "Pattern is not comprehensive" : "Patterns are not comprehensive"); 124 | } 125 | 126 | public Function Match(Func predicate, Func, Func> func) { 127 | _predicates.Add(predicate); 128 | _funcs.Add(func); 129 | return this; 130 | } 131 | 132 | public Func ToFunc() { 133 | return Invoke; 134 | } 135 | } 136 | 137 | public class Function 138 | { 139 | private readonly List, Func>> _funcs 140 | = new List, Func>>(); 141 | 142 | private readonly List> _predicates 143 | = new List>(); 144 | 145 | public TResult Invoke(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6) { 146 | Debug.Assert(_predicates.Count == _funcs.Count); 147 | for (var i = 0; i < _funcs.Count; i++) { 148 | if (_predicates[i](arg1, arg2, arg3, arg4, arg5, arg6)) { 149 | return _funcs[i](this)(arg1, arg2, arg3, arg4, arg5, arg6); 150 | } 151 | } 152 | throw new NotSupportedException(_predicates.Count == 1 ? "Pattern is not comprehensive" : "Patterns are not comprehensive"); 153 | } 154 | 155 | public Function Match(Func predicate, 156 | Func, Func> func) { 157 | _predicates.Add(predicate); 158 | _funcs.Add(func); 159 | return this; 160 | } 161 | 162 | public Func ToFunc() { 163 | return Invoke; 164 | } 165 | } 166 | 167 | public class Function 168 | { 169 | private readonly List, Func>> _funcs 170 | = new List, Func>>(); 171 | 172 | private readonly List> _predicates 173 | = new List>(); 174 | 175 | public TResult Invoke(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7) { 176 | Debug.Assert(_predicates.Count == _funcs.Count); 177 | for (var i = 0; i < _funcs.Count; i++) { 178 | if (_predicates[i](arg1, arg2, arg3, arg4, arg5, arg6, arg7)) { 179 | return _funcs[i](this)(arg1, arg2, arg3, arg4, arg5, arg6, arg7); 180 | } 181 | } 182 | throw new NotSupportedException(_predicates.Count == 1 ? "Pattern is not comprehensive" : "Patterns are not comprehensive"); 183 | } 184 | 185 | public Function Match(Func predicate, 186 | Func, Func> func) { 187 | _predicates.Add(predicate); 188 | _funcs.Add(func); 189 | return this; 190 | } 191 | 192 | public Func ToFunc() { 193 | return Invoke; 194 | } 195 | } 196 | 197 | public class Function 198 | { 199 | private readonly List, Func>> _funcs 200 | = new List, Func>>(); 201 | 202 | private readonly List> _predicates 203 | = new List>(); 204 | 205 | public TResult Invoke(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8) { 206 | Debug.Assert(_predicates.Count == _funcs.Count); 207 | for (var i = 0; i < _funcs.Count; i++) { 208 | if (_predicates[i](arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)) { 209 | return _funcs[i](this)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); 210 | } 211 | } 212 | throw new NotSupportedException(_predicates.Count == 1 ? "Pattern is not comprehensive" : "Patterns are not comprehensive"); 213 | } 214 | 215 | public Function Match(Func predicate, 216 | Func, Func> func) { 217 | _predicates.Add(predicate); 218 | _funcs.Add(func); 219 | return this; 220 | } 221 | 222 | public Func ToFunc() { 223 | return Invoke; 224 | } 225 | } 226 | 227 | public class Function 228 | { 229 | private readonly List, Func>> _funcs 230 | = new List, Func>>(); 231 | 232 | private readonly List> _predicates 233 | = new List>(); 234 | 235 | public TResult Invoke(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, TArg9 arg9) { 236 | Debug.Assert(_predicates.Count == _funcs.Count); 237 | for (var i = 0; i < _funcs.Count; i++) { 238 | if (_predicates[i](arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)) { 239 | return _funcs[i](this)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); 240 | } 241 | } 242 | throw new NotSupportedException(_predicates.Count == 1 ? "Pattern is not comprehensive" : "Patterns are not comprehensive"); 243 | } 244 | 245 | public Function Match(Func predicate, 246 | Func, Func> func) { 247 | _predicates.Add(predicate); 248 | _funcs.Add(func); 249 | return this; 250 | } 251 | 252 | public Func ToFunc() { 253 | return Invoke; 254 | } 255 | } 256 | } -------------------------------------------------------------------------------- /src/PatternMatchingExtension/MatchExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace PatternMatchingExtension 6 | { 7 | public static class MatchExpression 8 | { 9 | public static bool IsEmpty(this IList target) { 10 | return target.Count == 0; 11 | } 12 | 13 | public static TResult Match(this IList target, Func, TResult> func) { 14 | return func(target.First(), target.Skip(1).TakeWhile(x => true).ToList()); 15 | } 16 | 17 | public static Tuple Match(this object target, Action action) { 18 | return new Tuple(target, false).Match(action); 19 | } 20 | 21 | public static Tuple Match(this Tuple target, Action action) { 22 | //just simplely support two-tuple 23 | 24 | if (target.Item2) { 25 | return target; 26 | } 27 | 28 | var isMatchCompleted = false; 29 | 30 | var props = target.Item1.GetType().GetProperties(); 31 | var values = new object[2]; 32 | 33 | for (var i = 0; i < 2; i++) { 34 | values[i] = props[i].GetValue(target.Item1); 35 | } 36 | 37 | if (values[0] is T1 && values[1] is T2) { 38 | isMatchCompleted = true; 39 | action((T1)values[0], (T2)values[1]); 40 | } 41 | 42 | return new Tuple(target.Item1, isMatchCompleted); 43 | } 44 | 45 | public static Tuple Match(this Tuple target, Action action) { 46 | if (target.Item2) { 47 | return target; 48 | } 49 | 50 | var isMatchCompleted = false; 51 | if (target.Item1 is T1) { 52 | isMatchCompleted = true; 53 | action((T1)target.Item1); 54 | } 55 | 56 | return new Tuple(target.Item1, isMatchCompleted); 57 | } 58 | 59 | public static Tuple Match(this object target, Action action) { 60 | return new Tuple(target, false).Match(action); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/PatternMatchingExtension/PatternMatchingExtension.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {C8FC75A1-A78C-4A8D-B810-BB124EAF373C} 8 | Library 9 | Properties 10 | PatternMatchingExtension 11 | PatternMatchingExtension 12 | v4.6.1 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 61 | -------------------------------------------------------------------------------- /src/PatternMatchingExtension/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("PatternMatchingExtension")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("PatternMatchingExtension")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | //将 ComVisible 设置为 false 将使此程序集中的类型 18 | //对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("c8fc75a1-a78c-4a8d-b810-bb124eaf373c")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, 33 | // 方法是按如下所示使用“*”: : 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/PatternMatchingExtension/_.cs: -------------------------------------------------------------------------------- 1 | namespace PatternMatchingExtension 2 | { 3 | public class _ : object { } 4 | } --------------------------------------------------------------------------------