├── .gitattributes ├── .gitignore ├── LICENSE.md ├── Puresharp ├── IPuresharp │ ├── Authentic.cs │ ├── Authority.cs │ ├── Bypass.cs │ ├── IPuresharp.csproj │ ├── IPuresharp.snk │ ├── IPuresharp.xml │ ├── Importation.cs │ ├── Intermediate.cs │ ├── Machine.cs │ ├── Mono │ │ └── Cecil │ │ │ ├── Cil │ │ │ ├── Branch.Scope.cs │ │ │ ├── Branch.cs │ │ │ └── Lock.cs │ │ │ ├── __AssemblyDefinition.cs │ │ │ ├── __FieldDefinition.cs │ │ │ ├── __GenericParameter.cs │ │ │ ├── __MethodBody.cs │ │ │ ├── __MethodDefinition.cs │ │ │ ├── __MethodReference.cs │ │ │ ├── __ModuleDefinition.cs │ │ │ ├── __TypeDefinition.cs │ │ │ └── __TypeReference.cs │ ├── Program.cs │ ├── Properties │ │ ├── AssemblyInfo.cs │ │ ├── PublishProfiles │ │ │ └── FolderProfile.pubxml │ │ └── launchSettings.json │ └── System │ │ └── Collections │ │ └── Generic │ │ └── __Dictionary.cs ├── Puresharp.Debugger │ ├── Program.cs │ └── Puresharp.Debugger.csproj ├── Puresharp.sln └── Puresharp │ ├── Activation │ └── Activator.cs │ ├── Activity │ ├── Activity.cs │ └── IActivity.cs │ ├── Addin.cs │ ├── Addon.cs │ ├── Advice │ ├── Advice.cs │ └── IAdvice.cs │ ├── Advisor │ ├── Advisor.After.IReturning.cs │ ├── Advisor.After.Returning.cs │ ├── Advisor.After.cs │ ├── Advisor.Execution.Returning.Value.cs │ ├── Advisor.Execution.Returning.cs │ ├── Advisor.Execution.Throwing.cs │ ├── Advisor.Execution.cs │ ├── Advisor.Generator.cs │ ├── Advisor.IAfter.cs │ ├── Advisor.IGenerator.cs │ ├── Advisor.IParameter.cs │ ├── Advisor.Invocation.cs │ ├── Advisor.Parameter.IVisitor.cs │ ├── Advisor.Parameter.cs │ ├── Advisor.Sequence.Factory.cs │ ├── Advisor.Sequence.cs │ ├── Advisor.cs │ └── __Advisor.cs │ ├── Aspect │ ├── Aspect.Activity.cs │ ├── Aspect.Addin.cs │ ├── Aspect.Addon.cs │ ├── Aspect.Listener.cs │ ├── Aspect.Monitor.cs │ ├── Aspect.cs │ ├── Directory │ │ ├── Aspect.Directory.Entry.cs │ │ └── Aspect.Directory.cs │ └── __Aspect.cs │ ├── Attribute │ └── Attribute.cs │ ├── Audience │ ├── IAudition.cs │ ├── IListenable.cs │ ├── IListener.cs │ └── Listener.cs │ ├── Communication │ └── Communication.cs │ ├── Composition │ ├── Composition.IVisitor.cs │ ├── Composition.Visitor.cs │ ├── Composition.cs │ ├── Container.Converter.cs │ ├── Container.Lazy.cs │ ├── Container.Map.cs │ ├── Container.Mapping.cs │ ├── Container.Proxy.cs │ ├── Container.cs │ ├── IContainer.cs │ ├── IModule.cs │ ├── IResolver.cs │ ├── ISetup.cs │ ├── Module.cs │ ├── Reservation.cs │ ├── Resolver.cs │ └── Setup.cs │ ├── Concurrency.cs │ ├── Data │ ├── Data.Collection.cs │ ├── Data.Enumerable.Enumerator.cs │ ├── Data.Enumerable.cs │ ├── Data.IStorage.cs │ ├── Data.IStore.cs │ ├── Data.Map.cs │ ├── Data.Store.cs │ └── Data.cs │ ├── Directory │ ├── Directory.Audition.cs │ ├── Directory.cs │ └── IDirectory.cs │ ├── Extension.cs │ ├── IFormatter.cs │ ├── Instantiation.cs │ ├── Metadata.cs │ ├── Pointcut │ ├── Pointcut.And.cs │ ├── Pointcut.Not.cs │ ├── Pointcut.Or.cs │ └── Pointcut.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── Proxy │ ├── Override.cs │ └── Proxy.cs │ ├── Puresharp.csproj │ ├── Puresharp.snk │ ├── Puresharp.xml │ ├── Runtime │ ├── Declaration.cs │ ├── Definition.cs │ ├── Dictionary │ │ ├── Runtime.Dictionary.Inherited.cs │ │ └── Runtime.Dictionary.cs │ ├── Runtime.Inventory.cs │ ├── Runtime.Lookup.cs │ └── Runtime.cs │ ├── Singleton │ └── Singleton.cs │ ├── Startup.cs │ ├── System │ ├── Collections │ │ └── Generic │ │ │ ├── Collection.cs │ │ │ ├── Enumerable.Enumerator.cs │ │ │ ├── Enumerable.cs │ │ │ └── Resource.cs │ ├── Linq │ │ └── Expressions │ │ │ ├── Parameter.cs │ │ │ └── __LambdaExpression.cs │ ├── Reflection │ │ ├── Authority.cs │ │ ├── Emit │ │ │ ├── __BinaryReader.cs │ │ │ ├── __ILGenerator.cs │ │ │ ├── __Label.cs │ │ │ └── __ModuleBuilder.cs │ │ ├── Gallery.cs │ │ ├── Instruction.cs │ │ ├── Signature.cs │ │ ├── __AssemblyName.cs │ │ ├── __MethodBase.cs │ │ ├── __ParameterInfo.cs │ │ └── __Type.cs │ ├── Resource.cs │ ├── Runtime │ │ ├── Closure.Function.cs │ │ ├── Closure.Routine.cs │ │ └── Closure.cs │ └── __AppDomain.cs │ ├── Visitation │ ├── IVisitable.cs │ ├── IVisitor.cs │ ├── Visitor.cs │ └── __IVisitable.cs │ └── Weave │ ├── IWeave.cs │ ├── Weave.Connection.cs │ ├── Weave.IConnection.cs │ └── Weave.cs └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.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 | [Xx]64/ 19 | [Xx]86/ 20 | [Bb]uild/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | 85 | # Visual Studio profiler 86 | *.psess 87 | *.vsp 88 | *.vspx 89 | *.sap 90 | 91 | # TFS 2012 Local Workspace 92 | $tf/ 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | *.DotSettings.user 101 | 102 | # JustCode is a .NET coding add-in 103 | .JustCode 104 | 105 | # TeamCity is a build add-in 106 | _TeamCity* 107 | 108 | # DotCover is a Code Coverage Tool 109 | *.dotCover 110 | 111 | # NCrunch 112 | _NCrunch_* 113 | .*crunch*.local.xml 114 | nCrunchTemp_* 115 | 116 | # MightyMoose 117 | *.mm.* 118 | AutoTest.Net/ 119 | 120 | # Web workbench (sass) 121 | .sass-cache/ 122 | 123 | # Installshield output folder 124 | [Ee]xpress/ 125 | 126 | # DocProject is a documentation generator add-in 127 | DocProject/buildhelp/ 128 | DocProject/Help/*.HxT 129 | DocProject/Help/*.HxC 130 | DocProject/Help/*.hhc 131 | DocProject/Help/*.hhk 132 | DocProject/Help/*.hhp 133 | DocProject/Help/Html2 134 | DocProject/Help/html 135 | 136 | # Click-Once directory 137 | publish/ 138 | 139 | # Publish Web Output 140 | *.[Pp]ublish.xml 141 | *.azurePubxml 142 | 143 | # TODO: Un-comment the next line if you do not want to checkin 144 | # your web deploy settings because they may include unencrypted 145 | # passwords 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # NuGet Packages 150 | *.nupkg 151 | # The packages folder can be ignored because of Package Restore 152 | **/packages/* 153 | # except build/, which is used as an MSBuild target. 154 | !**/packages/build/ 155 | # Uncomment if necessary however generally it will be regenerated when needed 156 | #!**/packages/repositories.config 157 | # NuGet v3's project.json files produces more ignoreable files 158 | *.nuget.props 159 | *.nuget.targets 160 | 161 | # Microsoft Azure Build Output 162 | csx/ 163 | *.build.csdef 164 | 165 | # Microsoft Azure Emulator 166 | ecf/ 167 | rcf/ 168 | 169 | # Windows Store app package directory 170 | AppPackages/ 171 | BundleArtifacts/ 172 | 173 | # Visual Studio cache files 174 | # files ending in .cache can be ignored 175 | *.[Cc]ache 176 | # but keep track of directories ending in .cache 177 | !*.[Cc]ache/ 178 | 179 | # Others 180 | ClientBin/ 181 | [Ss]tyle[Cc]op.* 182 | ~$* 183 | *~ 184 | *.dbmdl 185 | *.dbproj.schemaview 186 | *.pfx 187 | *.publishsettings 188 | node_modules/ 189 | orleans.codegen.cs 190 | 191 | # RIA/Silverlight projects 192 | Generated_Code/ 193 | 194 | # Backup & report files from converting an old project file 195 | # to a newer Visual Studio version. Backup files are not needed, 196 | # because we have git ;-) 197 | _UpgradeReport_Files/ 198 | Backup*/ 199 | UpgradeLog*.XML 200 | UpgradeLog*.htm 201 | 202 | # SQL Server files 203 | *.mdf 204 | *.ldf 205 | 206 | # Business Intelligence projects 207 | *.rdl.data 208 | *.bim.layout 209 | *.bim_*.settings 210 | 211 | # Microsoft Fakes 212 | FakesAssemblies/ 213 | 214 | # GhostDoc plugin setting file 215 | *.GhostDoc.xml 216 | 217 | # Node.js Tools for Visual Studio 218 | .ntvs_analysis.dat 219 | 220 | # Visual Studio 6 build log 221 | *.plg 222 | 223 | # Visual Studio 6 workspace options file 224 | *.opt 225 | 226 | # Visual Studio LightSwitch build output 227 | **/*.HTMLClient/GeneratedArtifacts 228 | **/*.DesktopClient/GeneratedArtifacts 229 | **/*.DesktopClient/ModelManifest.xml 230 | **/*.Server/GeneratedArtifacts 231 | **/*.Server/ModelManifest.xml 232 | _Pvt_Extensions 233 | 234 | # LightSwitch generated files 235 | GeneratedArtifacts/ 236 | ModelManifest.xml 237 | 238 | # Paket dependency manager 239 | .paket/paket.exe 240 | 241 | # FAKE - F# Make 242 | .fake/ 243 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Tony THONG 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 | -------------------------------------------------------------------------------- /Puresharp/IPuresharp/Bypass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Runtime.CompilerServices; 4 | using Mono.Cecil; 5 | using Puresharp; 6 | 7 | namespace IPuresharp 8 | { 9 | static internal class Bypass 10 | { 11 | static public bool Match(TypeDefinition type) 12 | { 13 | if (type.Name.StartsWith("<")) { return true; } 14 | if (type.IsInterface) { return true; } 15 | if (type.IsValueType) { return true; } 16 | if (type.Name == Program.Module) { return true; } 17 | if (type.Is()) { return true; } 18 | if (type.Is()) { return true; } 19 | if (type.Is()) { return true; } 20 | if (type.Is()) { return true; } 21 | if (type.Is()) { return true; } 22 | if (type.Is()) { return true; } 23 | if (type.Is()) { return true; } 24 | if (type.CustomAttributes.Any(_Attribute => _Attribute.AttributeType.Name == "GeneratedCodeAttribute")) { return true; } 25 | return false; 26 | } 27 | 28 | static public bool Match(MethodDefinition method) 29 | { 30 | if (method.Name.StartsWith("<")) { return true; } 31 | if (method.Body == null) { return true; } 32 | if (method.IsConstructor && method.IsStatic) { return true; } 33 | if (method.Parameters.Any(_Parameter => _Parameter.ParameterType.IsByReference)) { return true; } 34 | if (method.CustomAttributes.Any(_Attribute => _Attribute.AttributeType.Name == "GeneratedCodeAttribute")) { return true; } 35 | return false; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Puresharp/IPuresharp/IPuresharp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net452 6 | true 7 | IPuresharp.snk 8 | 5.0.5 9 | Tony THONG 10 | Tony THONG 11 | 5.0.5 12 | 5.0.5 13 | Tony THONG 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Puresharp/IPuresharp/IPuresharp.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Puresharper/Puresharp/c08233453a2732bb8835175ceeaae243e5e5d72f/Puresharp/IPuresharp/IPuresharp.snk -------------------------------------------------------------------------------- /Puresharp/IPuresharp/IPuresharp.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | IPuresharp 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Puresharp/IPuresharp/Importation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Mono.Cecil; 5 | 6 | namespace IPuresharp 7 | { 8 | internal class Importation 9 | { 10 | private ModuleDefinition m_Module; 11 | private Dictionary m_Dictionary; 12 | 13 | public Importation(IGenericParameterProvider source, IGenericParameterProvider destination) 14 | { 15 | this.m_Module = source.Module; 16 | this.m_Dictionary = new Dictionary(); 17 | var _source = (source is MethodReference ? (source as MethodReference).DeclaringType.GenericParameters.Concat(source.GenericParameters) : source.GenericParameters).ToArray(); 18 | var _destination = (destination is MethodReference ? (destination as MethodReference).DeclaringType.GenericParameters.Concat(destination.GenericParameters) : destination.GenericParameters).ToArray(); 19 | for (var _index = 0; _index < _source.Length; _index++) { this.m_Dictionary.Add(_source[_index], _destination[_index]); } 20 | } 21 | 22 | public TypeReference this[TypeReference type] 23 | { 24 | get 25 | { 26 | if (type.IsByReference) { return new ByReferenceType(this[(type as ByReferenceType).ElementType]); } 27 | if (this.m_Dictionary.TryGetValue(type, out var _type)) { return _type; } 28 | if (type is GenericInstanceType) { return this.m_Module.Import(type.Resolve()).MakeGenericType((type as GenericInstanceType).GenericArguments.Select(_Type => this[_Type])); } 29 | return type; 30 | } 31 | } 32 | 33 | private TypeReference this[TypeReference type, GenericInstanceMethod method] 34 | { 35 | get 36 | { 37 | if (type.IsByReference) { return new ByReferenceType(this[(type as ByReferenceType).ElementType, method]); } 38 | if (this.m_Dictionary.TryGetValue(type, out var _type)) { return _type; } 39 | if (type is GenericInstanceType) { return this.m_Module.Import(type.Resolve()).MakeGenericType((type as GenericInstanceType).GenericArguments.Select(_Type => this[_Type, method])); } 40 | if (type.Name.StartsWith("!!")) { return method.Resolve().GenericParameters[int.Parse(type.Name.Substring(2))]; } 41 | return type; 42 | } 43 | } 44 | 45 | public MethodReference this[MethodReference method] 46 | { 47 | get 48 | { 49 | if (method is GenericInstanceMethod) 50 | { 51 | if (method.DeclaringType is GenericInstanceType) 52 | { 53 | var test = this.m_Module.Import(method.Resolve()).MakeHostInstanceGeneric((this[method.DeclaringType] as GenericInstanceType).GenericArguments.ToArray()); 54 | var m = test.MakeGenericMethod((method as GenericInstanceMethod).GenericArguments.Select(_Type => this[_Type]).ToArray()); 55 | //var k = this.m_Module.Import(method.Resolve()).MakeGenericMethod((method as GenericInstanceMethod).GenericArguments.Select(_Type => this[_Type]).ToArray()); 56 | //var m = k.MakeHostInstanceGeneric((this[method.DeclaringType] as GenericInstanceType).GenericArguments.ToArray()); 57 | //m.DeclaringType = this[method.DeclaringType, m as GenericInstanceMethod]; 58 | //m.ReturnType = this[method.ReturnType, m as GenericInstanceMethod]; 59 | //foreach (var p in method.Parameters) { m.Parameters[p.Index].ParameterType = this[p.ParameterType, m as GenericInstanceMethod]; } 60 | return m; 61 | } 62 | // var m = new MethodReference(method.Name, this[method.ReturnType], this[method.DeclaringType]) 63 | // { 64 | // HasThis = method.HasThis, 65 | // ExplicitThis = method.ExplicitThis, 66 | // CallingConvention = method.CallingConvention 67 | //}; 68 | // foreach (var p in method.Parameters) { m.Parameters.Add(new ParameterDefinition(p.Name, p.Attributes, this[p.ParameterType])); } 69 | var oo = this.m_Module.Import(method.Resolve()).MakeGenericMethod((method as GenericInstanceMethod).GenericArguments.Select(_Type => this[_Type]).ToArray()); 70 | //oo.ReturnType = this[method.ReturnType, m as GenericInstanceMethod]; 71 | //foreach (var p in method.Parameters) { oo.Parameters[p.Index].ParameterType = this[p.ParameterType, oo as GenericInstanceMethod]; } 72 | return oo; 73 | 74 | //var m = new MethodReference(method.Name, this[method.ReturnType], this[method.DeclaringType]) 75 | //{ 76 | // HasThis = method.HasThis, 77 | // ExplicitThis = method.ExplicitThis, 78 | // CallingConvention = method.CallingConvention 79 | //}; 80 | // foreach (var p in method.Parameters) { m.Parameters.Add(new ParameterDefinition(p.Name, p.Attributes, this[p.ParameterType])); } 81 | // foreach (var p in method.Parameters) { m.Parameters.Add(new ParameterDefinition(p.Name, p.Attributes, this[p.ParameterType])); } 82 | //return method; 83 | } 84 | else 85 | { 86 | if (method.DeclaringType is GenericInstanceType) { return this.m_Module.Import(method.Resolve()).MakeHostInstanceGeneric((this[method.DeclaringType] as GenericInstanceType).GenericArguments.Select(_Type => this[_Type]).ToArray()); } 87 | return method; 88 | } 89 | } 90 | } 91 | 92 | public FieldReference this[FieldReference field] 93 | { 94 | get { return field.DeclaringType is GenericInstanceType ? new FieldReference(field.Name, this[field.FieldType], this[field.DeclaringType]) : field; } 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /Puresharp/IPuresharp/Mono/Cecil/Cil/Branch.Scope.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using Mono; 5 | using Mono.Cecil; 6 | 7 | namespace Mono.Cecil.Cil 8 | { 9 | internal partial class Branch 10 | { 11 | private class Scope : IDisposable 12 | { 13 | private Branch m_Branch; 14 | 15 | public Scope(Branch branch) 16 | { 17 | this.m_Branch = branch; 18 | } 19 | 20 | public void Dispose() 21 | { 22 | List _item; 23 | if (Branch.m_Dictionary.TryGetValue(this.m_Branch.Body, out _item)) { _item.Add(this.m_Branch); } 24 | else 25 | { 26 | _item = new List(); 27 | _item.Add(this.m_Branch); 28 | Branch.m_Dictionary.Add(this.m_Branch.Body, _item); 29 | } 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Puresharp/IPuresharp/Mono/Cecil/Cil/Branch.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using Mono; 6 | using Mono.Cecil; 7 | 8 | namespace Mono.Cecil.Cil 9 | { 10 | internal partial class Branch 11 | { 12 | static private Dictionary> m_Dictionary = new Dictionary>(); 13 | 14 | static public Branch Query(MethodBody body) 15 | { 16 | if (Branch.m_Dictionary.TryGetValue(body, out var _item)) 17 | { 18 | var _branch = _item.Last(); 19 | _item.Remove(_branch); 20 | if (_item.Count == 0) { Branch.m_Dictionary.Remove(body); } 21 | return _branch; 22 | } 23 | return null; 24 | } 25 | 26 | public readonly MethodBody Body; 27 | public readonly Instruction Instruction; 28 | 29 | public Branch(MethodBody body, OpCode branch) 30 | { 31 | this.Body = body; 32 | this.Instruction = Mono.Cecil.Cil.Instruction.Create(branch, Mono.Cecil.Cil.Instruction.Create(OpCodes.Nop)); 33 | } 34 | 35 | public IDisposable Begin() 36 | { 37 | this.Body.Add(this.Instruction); 38 | return new Branch.Scope(this); 39 | } 40 | 41 | public void Finialize(Instruction instruction) 42 | { 43 | this.Instruction.Operand = instruction; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Puresharp/IPuresharp/Mono/Cecil/Cil/Lock.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Threading; 4 | using Puresharp; 5 | 6 | namespace Mono.Cecil.Cil 7 | { 8 | internal partial class Lock : IDisposable 9 | { 10 | static private MethodInfo m_Enter = Puresharp.Metadata.Method(() => Monitor.Enter(Metadata.Value)); 11 | static private MethodInfo m_Exit = Puresharp.Metadata.Method(() => Monitor.Exit(Metadata.Value)); 12 | 13 | private MethodBody m_Body; 14 | private FieldReference m_Field; 15 | 16 | public Lock(MethodBody body, FieldReference field) 17 | { 18 | this.m_Body = body; 19 | this.m_Field = field; 20 | if (field.Resolve().IsStatic) 21 | { 22 | body.Emit(OpCodes.Ldsfld, field); 23 | body.Emit(OpCodes.Call, Lock.m_Enter); 24 | } 25 | else 26 | { 27 | body.Emit(OpCodes.Ldarg_0); 28 | body.Emit(OpCodes.Ldsfld, field); 29 | body.Emit(OpCodes.Call, Lock.m_Enter); 30 | } 31 | } 32 | 33 | public void Dispose() 34 | { 35 | if (this.m_Field.Resolve().IsStatic) 36 | { 37 | this.m_Body.Emit(OpCodes.Ldsfld, this.m_Field); 38 | this.m_Body.Emit(OpCodes.Call, Lock.m_Enter); 39 | } 40 | else 41 | { 42 | this.m_Body.Emit(OpCodes.Ldarg_0); 43 | this.m_Body.Emit(OpCodes.Ldsfld, this.m_Field); 44 | this.m_Body.Emit(OpCodes.Call, Lock.m_Enter); 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Puresharp/IPuresharp/Mono/Cecil/__AssemblyDefinition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Linq.Expressions; 4 | using Mono; 5 | using Puresharp; 6 | 7 | namespace Mono.Cecil 8 | { 9 | static internal class __AssemblyDefinition 10 | { 11 | static public CustomAttribute Attribute(this AssemblyDefinition assembly) 12 | where T : Attribute 13 | { 14 | var _attribute = new CustomAttribute(assembly.MainModule.Import(typeof(T).GetConstructor(Type.EmptyTypes))); 15 | assembly.CustomAttributes.Add(_attribute); 16 | return _attribute; 17 | } 18 | 19 | static public CustomAttribute Attribute(this AssemblyDefinition assembly, Expression> expression) 20 | where T : Attribute 21 | { 22 | var _constructor = (expression.Body as NewExpression).Constructor; 23 | var _attribute = new CustomAttribute(assembly.MainModule.Import(_constructor)); 24 | foreach (var _argument in (expression.Body as NewExpression).Arguments) { _attribute.ConstructorArguments.Add(new CustomAttributeArgument(assembly.MainModule.Import(_argument.Type), Expression.Lambda>(Expression.Convert(_argument, Metadata.Type)).Compile()())); } 25 | assembly.CustomAttributes.Add(_attribute); 26 | return _attribute; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Puresharp/IPuresharp/Mono/Cecil/__FieldDefinition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Linq.Expressions; 4 | using Mono; 5 | using Mono.Collections.Generic; 6 | using Puresharp; 7 | 8 | namespace Mono.Cecil 9 | { 10 | static internal class __FieldDefinition 11 | { 12 | static public CustomAttribute Attribute(this FieldDefinition field) 13 | where T : Attribute 14 | { 15 | var _attribute = new CustomAttribute(field.Module.Import(typeof(T).GetConstructor(Type.EmptyTypes))); 16 | field.CustomAttributes.Add(_attribute); 17 | return _attribute; 18 | } 19 | 20 | static public CustomAttribute Attribute(this FieldDefinition field, Expression> expression) 21 | where T : Attribute 22 | { 23 | var _constructor = (expression.Body as NewExpression).Constructor; 24 | var _attribute = new CustomAttribute(field.Module.Import(_constructor)); 25 | foreach (var _argument in (expression.Body as NewExpression).Arguments) { _attribute.ConstructorArguments.Add(new CustomAttributeArgument(field.Module.Import(_argument.Type), Expression.Lambda>(Expression.Convert(_argument, Metadata.Type)).Compile()())); } 26 | field.CustomAttributes.Add(_attribute); 27 | return _attribute; 28 | } 29 | 30 | static public FieldReference Relative(this FieldDefinition field) 31 | { 32 | var _type = field.DeclaringType.Relative(); 33 | if (_type == field.DeclaringType) { return field; } 34 | return new FieldReference(field.Name, field.DeclaringType.Module.Import(field.FieldType), _type); 35 | } 36 | 37 | static public TypeReference Relative(this TypeDefinition type) 38 | { 39 | //if (type.DeclaringType == null) 40 | //{ 41 | if (type.GenericParameters.Count == 0) { return type; } 42 | return type.MakeGenericType(type.GenericParameters); 43 | //} 44 | //else 45 | //{ 46 | // var _type = type.DeclaringType.Relative(); 47 | // _type = _type.Resolve().NestedTypes.Single(_Type => _Type.Name == type.Name); 48 | // if (_type.GenericParameters.Count == 0) { return _type; } 49 | // return _type.MakeGenericType(_type.GenericParameters); 50 | //} 51 | } 52 | 53 | static public MethodReference Relative(this MethodDefinition method) 54 | { 55 | var _type = method.DeclaringType.Relative(); 56 | if (_type == method.DeclaringType) { return method; } 57 | var _method = new MethodReference(method.Name, method.Module.Import(method.ReturnType), _type); 58 | foreach (var _parameter in method.Parameters) { _method.Parameters.Add(new ParameterDefinition(_parameter.Name, _parameter.Attributes, _parameter.ParameterType)); } 59 | return _method; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Puresharp/IPuresharp/Mono/Cecil/__GenericParameter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Mono; 3 | 4 | namespace Mono.Cecil 5 | { 6 | static internal class __GenericParameter 7 | { 8 | static public GenericParameter Copy(this GenericParameter parameter, IGenericParameterProvider owner) 9 | { 10 | var _parameter = new GenericParameter(parameter.Name, owner); 11 | _parameter.Attributes = parameter.Attributes; 12 | foreach (var _type in parameter.Constraints) { _parameter.Constraints.Add(_type); } 13 | foreach (var _attribute in parameter.CustomAttributes) { _parameter.CustomAttributes.Add(_attribute); } 14 | _parameter.HasDefaultConstructorConstraint = parameter.HasDefaultConstructorConstraint; 15 | _parameter.HasNotNullableValueTypeConstraint = parameter.HasNotNullableValueTypeConstraint; 16 | _parameter.HasReferenceTypeConstraint = parameter.HasReferenceTypeConstraint; 17 | _parameter.IsContravariant = parameter.IsContravariant; 18 | _parameter.IsCovariant = parameter.IsCovariant; 19 | _parameter.IsNonVariant = _parameter.IsNonVariant; 20 | _parameter.IsValueType = parameter.IsValueType; 21 | return _parameter; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Puresharp/IPuresharp/Mono/Cecil/__MethodReference.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using Mono.Cecil.Rocks; 5 | 6 | namespace Mono.Cecil 7 | { 8 | static internal class __MethodReference 9 | { 10 | static public MethodReference MakeGenericMethod(this MethodReference method, params TypeReference[] arguments) 11 | { 12 | //var _method = new GenericInstanceMethod(method); 13 | //foreach (var _argument in arguments) { _method.GenericArguments.Add(_argument); } 14 | //return _method; 15 | 16 | if (arguments.Length == 0) 17 | { 18 | return method; 19 | } 20 | 21 | if (method.GenericParameters.Count != arguments.Length) 22 | { 23 | throw new ArgumentException ("Invalid number of generic typearguments supplied"); 24 | } 25 | 26 | var genericTypeRef = new GenericInstanceMethod (method); 27 | foreach (var arg in arguments) 28 | { 29 | genericTypeRef.GenericArguments.Add (arg); 30 | } 31 | 32 | return genericTypeRef; 33 | } 34 | 35 | public static MethodReference MakeHostInstanceGeneric(this MethodReference self, params TypeReference[] arguments) 36 | { 37 | var reference = new MethodReference(self.Name, self.ReturnType, self.DeclaringType.MakeGenericInstanceType(arguments)) 38 | { 39 | HasThis = self.HasThis, 40 | ExplicitThis = self.ExplicitThis, 41 | CallingConvention = self.CallingConvention 42 | }; 43 | 44 | foreach (var generic_parameter in self.GenericParameters) 45 | { 46 | reference.GenericParameters.Add(new GenericParameter(generic_parameter.Name, reference)); 47 | } 48 | 49 | foreach (var parameter in self.Parameters) 50 | { 51 | //if (parameter.ParameterType is GenericParameter) 52 | //{ 53 | // var gp = parameter.ParameterType as GenericParameter; 54 | // if (gp.Owner == self.DeclaringType) 55 | // { 56 | // var import = new IPuresharp.Importation(gp.Owner, reference.DeclaringType.Resolve()); 57 | // reference.Parameters.Add(new ParameterDefinition(parameter.Name, parameter.Attributes, import[parameter.ParameterType])); 58 | // } 59 | // else 60 | // { 61 | // reference.Parameters.Add(new ParameterDefinition(parameter.Name, parameter.Attributes, parameter.ParameterType)); 62 | // } 63 | //} 64 | //else 65 | //{ 66 | reference.Parameters.Add(new ParameterDefinition(parameter.Name, parameter.Attributes, parameter.ParameterType)); 67 | //} 68 | } 69 | 70 | return reference; 71 | } 72 | 73 | //static public GenericInstanceMethod MakeGenericMethod(this MethodReference method, IEnumerable arguments) 74 | //{ 75 | // var _method = new GenericInstanceMethod(method); 76 | // foreach (var _argument in arguments) { _method.GenericArguments.Add(_argument); } 77 | // return _method; 78 | //} 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Puresharp/IPuresharp/Mono/Cecil/__ModuleDefinition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime; 3 | using System.Runtime.CompilerServices; 4 | using Mono; 5 | 6 | namespace Mono.Cecil 7 | { 8 | static internal class __ModuleDefinition 9 | { 10 | static public TypeDefinition Type(this ModuleDefinition module, string name, TypeAttributes attributes) 11 | { 12 | var _type = new TypeDefinition(null, name, attributes, module.TypeSystem.Object); 13 | module.Types.Add(_type); 14 | _type.Attribute(); 15 | _type.Attribute(); 16 | return _type; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Puresharp/IPuresharp/Mono/Cecil/__TypeReference.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace Mono.Cecil 7 | { 8 | static internal class __TypeReference 9 | { 10 | static public bool IsAssignableFrom(this TypeReference type, TypeDefinition speculative) 11 | { 12 | var _type = type.Resolve(); 13 | if (speculative.BaseType != null && speculative.BaseType.Resolve() == _type) { return true; } 14 | if (speculative.Interfaces.Any(_Type => _Type.InterfaceType.Resolve() == _type)) { return true; } 15 | return false; 16 | } 17 | 18 | static public bool Is(this TypeReference @this) 19 | { 20 | return @this.Is(@this.Module.Import(typeof(T))); 21 | } 22 | 23 | static public bool Is(this TypeReference @this, TypeReference type) 24 | { 25 | if (@this == null || type == null) { return false; } 26 | if (@this.FullName == type.FullName && @this.Module.Name == type.Module.Name) { return true; } 27 | return @this.Resolve().BaseType.Is(type) || @this.Resolve().Interfaces.Any(_Interface => _Interface.InterfaceType.Is(type)); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Puresharp/IPuresharp/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /Puresharp/IPuresharp/Properties/PublishProfiles/FolderProfile.pubxml: -------------------------------------------------------------------------------- 1 |  2 | 5 | 6 | 7 | FileSystem 8 | Release 9 | Any CPU 10 | netcoreapp2.1 11 | bin\Release\netcoreapp2.1\publish\ 12 | 13 | -------------------------------------------------------------------------------- /Puresharp/IPuresharp/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "IPuresharp": { 4 | "commandName": "Project", 5 | "commandLineArgs": "\"C:\\Users\\THONGTO\\source\\repos\\ConsoleApp10\\ConsoleApp10\\bin\\Debug\\ConsoleApp10.exe\"" 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /Puresharp/IPuresharp/System/Collections/Generic/__Dictionary.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace System.Collections.Generic 6 | { 7 | static internal class __Dictionary 8 | { 9 | static public TValue TryGetValue(this Dictionary dictionary, TKey key) 10 | where TValue : class 11 | { 12 | if (dictionary.TryGetValue(key, out var _value)) { return _value; } 13 | return _value; 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Puresharp/Puresharp.Debugger/Program.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System; 3 | using System.Diagnostics; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Net; 7 | using System.Reflection; 8 | using System.Text; 9 | 10 | namespace Puresharp.Debugger 11 | { 12 | public class Article 13 | { 14 | public string ID { get; set; } 15 | public int REF { get; set; } 16 | public DateTime Date { get; set; } 17 | } 18 | 19 | public interface IArticle 20 | { 21 | Article Rechercher(int number); 22 | int Add(int a, int b); 23 | } 24 | 25 | public class Article1 : IArticle 26 | { 27 | 28 | private Uri test = new Uri("http://www.micorosoft.com"); 29 | 30 | public Uri TEST 31 | { 32 | get { return this.test; } 33 | } 34 | 35 | public int Add(int a, int b) 36 | { 37 | return a + b; 38 | } 39 | 40 | public Article Rechercher(int number) 41 | { 42 | return new Article() 43 | { 44 | ID = Guid.NewGuid().ToString(), 45 | REF = 28, 46 | Date = DateTime.Now 47 | }; 48 | } 49 | } 50 | 51 | public class Formatter : IFormatter 52 | { 53 | private Newtonsoft.Json.JsonSerializer m_g = Newtonsoft.Json.JsonSerializer.Create(); 54 | private Encoding m_Encoding = new UTF8Encoding(false); 55 | 56 | public T Deserialize(Stream stream) 57 | { 58 | try 59 | { 60 | //return JsonConvert.DeserializeObject(new StreamReader(stream, this.m_Encoding).ReadToEnd()); 61 | using (var jtr = new JsonTextReader(new StreamReader(stream, this.m_Encoding))) 62 | { 63 | return this.m_g.Deserialize(jtr); 64 | } 65 | } 66 | catch (Exception exception) { throw new ArgumentException("Unable to deserialize json request body.", exception); } 67 | } 68 | 69 | public void Serialize(Stream stream, T value) 70 | { 71 | //var sw = new StreamWriter(stream, this.m_Encoding); 72 | //var s = JsonConvert.SerializeObject(value); 73 | //sw.Write(s); 74 | //sw.Flush(); 75 | var sw = new StreamWriter(stream, this.m_Encoding); 76 | this.m_g.Serialize(sw, value); 77 | sw.Flush(); 78 | } 79 | } 80 | 81 | //public class Manager : Communication.IManager 82 | //{ 83 | // public int Code(Exception exception) 84 | // { 85 | // if (exception is ArgumentException) { return 400; } 86 | // if (exception is NotImplementedException) { return 501; } 87 | // return 500; 88 | // } 89 | //} 90 | 91 | //public class C1 : Communication.IConvention 92 | //{ 93 | // public Communication.IMapping this[MethodInfo method] 94 | // { 95 | // get 96 | // { 97 | // var _signature = method.GetParameters(); 98 | // return new Communication.Mapping 99 | // ( 100 | // $"/{method.DeclaringType.Name}/{method.Name}", 101 | // method.ReturnType != Metadata.Void && (_signature.Length > 1 || (_signature.Length > 0 && !_signature.Any(_Parameter => _Parameter.ParameterType == Metadata.Type || _Parameter.ParameterType == Metadata.Type || _Parameter.ParameterType == Metadata.Type || _Parameter.ParameterType == Metadata.Type || _Parameter.ParameterType == Metadata.Type || _Parameter.ParameterType == Metadata.Type || _Parameter.ParameterType == Metadata.Type || _Parameter.ParameterType == Metadata.Type || _Parameter.ParameterType == Metadata.Type || _Parameter.ParameterType == Metadata.Type || _Parameter.ParameterType == Metadata.Type || _Parameter.ParameterType == Metadata.Type || _Parameter.ParameterType == Metadata.Type || _Parameter.ParameterType == Metadata.Type))) ? Communication.Verbs.POST : Communication.Verbs.GET, 102 | // new Formatter(), 103 | // new Manager() 104 | // ); 105 | // } 106 | // } 107 | //} 108 | 109 | class Program 110 | { 111 | static void Main(string[] args) 112 | { 113 | 114 | var aaa = new Article1() as IArticle; 115 | 116 | //Console.WriteLine("Hello World!"); 117 | var _composition = new Composition() 118 | .Setup(() => null); 119 | var _composition2 = new Composition().Setup(() => new Article1()); 120 | _composition.Then(_composition2); 121 | //var _directory = new Communication.Directory(); 122 | //_directory.Add(); 123 | using (var _container = _composition.Materialize()) 124 | { 125 | using (var m = _container.Module()) 126 | { 127 | m.Value.Add(2, 3); 128 | } 129 | 130 | // using (var _communication = new Communication(_container, _directory, new C1())) 131 | // { 132 | // Console.ReadLine(); 133 | // } 134 | } 135 | } 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /Puresharp/Puresharp.Debugger/Puresharp.Debugger.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Puresharp/Puresharp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28306.52 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Puresharp", "Puresharp\Puresharp.csproj", "{3A912F4E-3D7F-4AD2-89FB-B552C9609921}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IPuresharp", "IPuresharp\IPuresharp.csproj", "{0E88BE3D-1F7C-4968-AC14-2BAEE0E02984}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Puresharp.Debugger", "Puresharp.Debugger\Puresharp.Debugger.csproj", "{221E3D7B-15CF-4961-A547-3189AF946AE7}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {3A912F4E-3D7F-4AD2-89FB-B552C9609921}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {3A912F4E-3D7F-4AD2-89FB-B552C9609921}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {3A912F4E-3D7F-4AD2-89FB-B552C9609921}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {3A912F4E-3D7F-4AD2-89FB-B552C9609921}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {0E88BE3D-1F7C-4968-AC14-2BAEE0E02984}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {0E88BE3D-1F7C-4968-AC14-2BAEE0E02984}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {0E88BE3D-1F7C-4968-AC14-2BAEE0E02984}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {0E88BE3D-1F7C-4968-AC14-2BAEE0E02984}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {221E3D7B-15CF-4961-A547-3189AF946AE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {221E3D7B-15CF-4961-A547-3189AF946AE7}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {221E3D7B-15CF-4961-A547-3189AF946AE7}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {221E3D7B-15CF-4961-A547-3189AF946AE7}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {709DBD8B-0718-4093-83BB-C2CDDE6F5C51} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Activation/Activator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | internal class Activator 6 | { 7 | private Func m_Activate; 8 | 9 | public Activator(T value) 10 | { 11 | this.m_Activate = new Func(() => value); 12 | } 13 | 14 | public Func Activate 15 | { 16 | get { return this.m_Activate; } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Activity/Activity.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | 5 | namespace Puresharp 6 | { 7 | public partial class Activity : IActivity 8 | { 9 | #if NET452 10 | static private string m_Token = Guid.NewGuid().ToString("N"); 11 | 12 | static private Dictionary Dictionary 13 | { 14 | get { return System.Runtime.Remoting.Messaging.CallContext.LogicalGetData(Activity.m_Token) as Dictionary; } 15 | set { System.Runtime.Remoting.Messaging.CallContext.LogicalSetData(Activity.m_Token, value); } 16 | } 17 | #else 18 | static private System.Threading.AsyncLocal> m_Storage = new System.Threading.AsyncLocal>(); 19 | 20 | static private Dictionary Dictionary 21 | { 22 | get { return Activity.m_Storage.Value; } 23 | set { Activity.m_Storage.Value = value; } 24 | } 25 | #endif 26 | 27 | static public Data.IStore Store() 28 | where T : class 29 | { 30 | var _dictionary = Activity.Dictionary; 31 | if (_dictionary == null) 32 | { 33 | _dictionary = new Dictionary(); 34 | var _store = new Data.Store(); 35 | _dictionary.Add(Metadata.Type, _store); 36 | Activity.Dictionary = _dictionary; 37 | return _store; 38 | } 39 | else 40 | { 41 | if (_dictionary.TryGetValue(Metadata.Type, out var _store)) { return _store as Data.IStore; } 42 | _store = new Data.Store(); 43 | _dictionary.Add(Metadata.Type, _store); 44 | return _store as Data.IStore; 45 | } 46 | } 47 | 48 | private IActivity m_Authentic; 49 | 50 | public Activity(IActivity authentic) 51 | { 52 | this.m_Authentic = authentic; 53 | } 54 | 55 | public IActivity Authentic 56 | { 57 | get { return this.m_Authentic; } 58 | } 59 | 60 | virtual public void Instance(T instance) 61 | { 62 | this.m_Authentic.Instance(instance); 63 | } 64 | 65 | virtual public void Argument(T value) 66 | { 67 | this.m_Authentic.Argument(value); 68 | } 69 | 70 | virtual public void Invoke() 71 | { 72 | this.m_Authentic.Invoke(); 73 | } 74 | 75 | virtual public T Invoke() 76 | { 77 | return this.m_Authentic.Invoke(); 78 | } 79 | 80 | public void Dispose() 81 | { 82 | this.m_Authentic.Dispose(); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Activity/IActivity.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | public interface IActivity : IDisposable 6 | { 7 | void Instance(T instance); 8 | void Argument(T value); 9 | void Invoke(); 10 | T Invoke(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Addin.cs: -------------------------------------------------------------------------------- 1 | //using System; 2 | 3 | //namespace Puresharp 4 | //{ 5 | // /// 6 | // /// Apply extension on target type as addin. 7 | // /// 8 | // [AttributeUsage(AttributeTargets.Assembly, AllowMultiple=true, Inherited=true)] 9 | // public class Addin : Attribute 10 | // { 11 | // /// 12 | // /// Extension. 13 | // /// 14 | // public readonly Type Extension; 15 | 16 | // /// 17 | // /// Type. 18 | // /// 19 | // public readonly Type Type; 20 | 21 | // /// 22 | // /// Declare an addin. 23 | // /// 24 | // /// Extension : must derived from Extension. 25 | // /// Type. 26 | // public Addin(Type extension, Type type) 27 | // { 28 | // this.Extension = extension; 29 | // this.Type = type; 30 | // } 31 | // } 32 | //} 33 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Addon.cs: -------------------------------------------------------------------------------- 1 | //using System; 2 | 3 | //namespace Puresharp 4 | //{ 5 | // [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true, Inherited = true)] 6 | // public class Addon : Attribute 7 | // { 8 | // public readonly Type Extension; 9 | // public readonly Type Type; 10 | 11 | // public Addon(Type extension, Type type) 12 | // { 13 | // this.Extension = extension; 14 | // this.Type = type; 15 | // } 16 | // } 17 | //} 18 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advice/Advice.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel; 3 | using System.Diagnostics; 4 | using System.Reflection; 5 | using System.Threading.Tasks; 6 | 7 | namespace Puresharp 8 | { 9 | /// 10 | /// Advice. 11 | /// 12 | public partial class Advice : IAdvice 13 | { 14 | [DebuggerHidden] 15 | [Browsable(false)] 16 | [EditorBrowsable(EditorBrowsableState.Never)] 17 | new static public bool Equals(object left, object right) 18 | { 19 | return object.Equals(left, right); 20 | } 21 | 22 | [DebuggerHidden] 23 | [Browsable(false)] 24 | [EditorBrowsable(EditorBrowsableState.Never)] 25 | new static public bool ReferenceEquals(object left, object right) 26 | { 27 | return object.ReferenceEquals(left, right); 28 | } 29 | 30 | static public Advisor.Generator For(MethodBase method) 31 | { 32 | return new Advisor.Generator(method); 33 | } 34 | 35 | void IAdvice.Instance(T instance) 36 | { 37 | } 38 | 39 | void IAdvice.Argument(ref T value) 40 | { 41 | } 42 | 43 | void IAdvice.Begin() 44 | { 45 | } 46 | 47 | void IAdvice.Await(MethodInfo method, Task task) 48 | { 49 | } 50 | 51 | void IAdvice.Await(MethodInfo method, Task task) 52 | { 53 | } 54 | 55 | void IAdvice.Continue() 56 | { 57 | } 58 | 59 | void IAdvice.Throw(ref Exception exception) 60 | { 61 | } 62 | 63 | void IAdvice.Throw(ref Exception exception, ref T value) 64 | { 65 | } 66 | 67 | void IAdvice.Return() 68 | { 69 | } 70 | 71 | void IAdvice.Return(ref T value) 72 | { 73 | } 74 | 75 | void IDisposable.Dispose() 76 | { 77 | } 78 | } 79 | } -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advice/IAdvice.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Threading.Tasks; 4 | 5 | namespace Puresharp 6 | { 7 | /// 8 | /// Advice. 9 | /// 10 | public interface IAdvice : IDisposable 11 | { 12 | /// 13 | /// Called when advice is bounded to instance method. 14 | /// 15 | /// Type of instance 16 | /// Instance 17 | void Instance(T value); 18 | 19 | /// 20 | /// Called for each argument passed. 21 | /// 22 | /// Type of argument 23 | /// Value 24 | void Argument(ref T value); 25 | 26 | /// 27 | /// Called before calling targeted method 28 | /// 29 | void Begin(); 30 | 31 | /// 32 | /// Called when method start awaiting asynchronous method without return value. 33 | /// 34 | /// Asynchronous method 35 | /// Task 36 | void Await(MethodInfo method, Task task); 37 | 38 | /// 39 | /// Called when method start awaiting asynchronous method with a return value. 40 | /// 41 | /// Type of return of the asynchronous method 42 | /// Asynchronous method 43 | /// Task 44 | void Await(MethodInfo method, Task task); 45 | 46 | /// 47 | /// Called when awaiting asynchronous method end. 48 | /// 49 | void Continue(); 50 | 51 | /// 52 | /// Called when method execution without return value throw an exception. 53 | /// 54 | /// Exception 55 | void Throw(ref Exception exception); 56 | 57 | /// 58 | /// Called when method execution with a return value throw an exception. 59 | /// 60 | /// Type of return value 61 | /// Exception 62 | /// Return value 63 | void Throw(ref Exception exception, ref T value); 64 | 65 | /// 66 | /// Called when method execution end without return value. 67 | /// 68 | void Return(); 69 | 70 | /// 71 | /// Called when method execution with a return value end. 72 | /// 73 | /// Type of return value 74 | /// Return value 75 | void Return(ref T value); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.After.IReturning.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | public partial class Advisor 6 | { 7 | public partial class After 8 | { 9 | public interface IReturning 10 | { 11 | Advisor.IAfter After { get; } 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.After.Returning.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | public partial class Advisor 6 | { 7 | public partial class After 8 | { 9 | public partial class Returning : Advisor.After.IReturning 10 | { 11 | private Advisor.IAfter m_After; 12 | 13 | internal Returning(Advisor.IAfter after) 14 | { 15 | this.m_After = after; 16 | } 17 | 18 | Advisor.IAfter Advisor.After.IReturning.After 19 | { 20 | get { return this.m_After; } 21 | } 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.After.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | public partial class Advisor 6 | { 7 | public partial class After : Advisor.IAfter 8 | { 9 | private Advisor.IGenerator m_Generator; 10 | 11 | internal After(Advisor.IGenerator generator) 12 | { 13 | this.m_Generator = generator; 14 | } 15 | 16 | Advisor.IGenerator Advisor.IAfter.Generator 17 | { 18 | get { return this.m_Generator; } 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.Execution.Returning.Value.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | using System.Reflection; 4 | 5 | namespace Puresharp 6 | { 7 | public partial class Advisor 8 | { 9 | public partial class Execution 10 | { 11 | public partial class Returning 12 | { 13 | public partial class Value 14 | { 15 | private MethodBase m_Method; 16 | private Expression m_Instance; 17 | private Collection m_Arguments; 18 | private Expression m_Return; 19 | 20 | internal Value(MethodBase method, Expression instance, Collection arguments, Expression @return) 21 | { 22 | this.m_Method = method; 23 | this.m_Instance = instance; 24 | this.m_Arguments = arguments; 25 | this.m_Return = @return; 26 | } 27 | 28 | public MethodBase Method 29 | { 30 | get { return this.m_Method; } 31 | } 32 | 33 | public Expression Instance 34 | { 35 | get { return this.m_Instance; } 36 | } 37 | 38 | public Collection Arguments 39 | { 40 | get { return this.m_Arguments; } 41 | } 42 | 43 | public Expression Return 44 | { 45 | get { return this.m_Return; } 46 | } 47 | } 48 | } 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.Execution.Returning.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | using System.Reflection; 4 | 5 | namespace Puresharp 6 | { 7 | public partial class Advisor 8 | { 9 | public partial class Execution 10 | { 11 | public partial class Returning 12 | { 13 | private MethodBase m_Method; 14 | private Expression m_Instance; 15 | private Collection m_Arguments; 16 | 17 | internal Returning(MethodBase method, Expression instance, Collection arguments) 18 | { 19 | this.m_Method = method; 20 | this.m_Instance = instance; 21 | this.m_Arguments = arguments; 22 | } 23 | 24 | public MethodBase Method 25 | { 26 | get { return this.m_Method; } 27 | } 28 | 29 | public Expression Instance 30 | { 31 | get { return this.m_Instance; } 32 | } 33 | 34 | public Collection Arguments 35 | { 36 | get { return this.m_Arguments; } 37 | } 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.Execution.Throwing.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace Puresharp 5 | { 6 | public partial class Advisor 7 | { 8 | public partial class Execution 9 | { 10 | public partial class Throwing 11 | { 12 | private Advisor.Invocation m_Invocation; 13 | private Expression m_Exception; 14 | 15 | public Throwing(Advisor.Invocation invocation, Expression exception) 16 | { 17 | this.m_Invocation = invocation; 18 | this.m_Exception = exception; 19 | } 20 | 21 | public Advisor.Invocation Invocation 22 | { 23 | get { return this.m_Invocation; } 24 | } 25 | 26 | public Expression Exception 27 | { 28 | get { return this.m_Exception; } 29 | } 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.Execution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | using System.Reflection; 4 | 5 | namespace Puresharp 6 | { 7 | public partial class Advisor 8 | { 9 | public partial class Execution 10 | { 11 | private MethodBase m_Method; 12 | private Expression m_Instance; 13 | private Collection m_Arguments; 14 | 15 | internal Execution(MethodBase method, Expression instance, Collection arguments) 16 | { 17 | this.m_Method = method; 18 | this.m_Instance = instance; 19 | this.m_Arguments = arguments; 20 | } 21 | 22 | public MethodBase Method 23 | { 24 | get { return this.m_Method; } 25 | } 26 | 27 | public Expression Instance 28 | { 29 | get { return this.m_Instance; } 30 | } 31 | 32 | public Collection Arguments 33 | { 34 | get { return this.m_Arguments; } 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.Generator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace Puresharp 5 | { 6 | public partial class Advisor 7 | { 8 | public class Generator : Advisor.IGenerator 9 | { 10 | private MethodBase m_Method; 11 | 12 | internal Generator(MethodBase method) 13 | { 14 | this.m_Method = method; 15 | } 16 | 17 | MethodBase Advisor.IGenerator.Method 18 | { 19 | get { return this.m_Method; } 20 | } 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.IAfter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | public partial class Advisor 6 | { 7 | public interface IAfter 8 | { 9 | Advisor.IGenerator Generator { get; } 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.IGenerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace Puresharp 5 | { 6 | public partial class Advisor 7 | { 8 | public interface IGenerator 9 | { 10 | MethodBase Method { get; } 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.IParameter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | public partial class Advisor 6 | { 7 | public interface IParameter 8 | { 9 | Advisor.IGenerator Generator { get; } 10 | } 11 | 12 | public interface IParameter 13 | where T : Attribute 14 | { 15 | Advisor.IGenerator Generator { get; } 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.Invocation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | using System.Reflection; 4 | 5 | namespace Puresharp 6 | { 7 | public partial class Advisor 8 | { 9 | public class Invocation 10 | { 11 | private MethodBase m_Method; 12 | private Expression m_Instance; 13 | private Collection m_Arguments; 14 | 15 | internal Invocation(MethodBase method, Expression instance, Collection arguments) 16 | { 17 | this.m_Method = method; 18 | this.m_Instance = instance; 19 | this.m_Arguments = arguments; 20 | } 21 | 22 | public MethodBase Method 23 | { 24 | get { return this.m_Method; } 25 | } 26 | 27 | public Expression Instance 28 | { 29 | get { return this.m_Instance; } 30 | } 31 | 32 | public Collection Arguments 33 | { 34 | get { return this.m_Arguments; } 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.Parameter.IVisitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | public partial class Advisor 6 | { 7 | public partial class Parameter 8 | { 9 | public interface IVisitor 10 | { 11 | void Visit(Func value); 12 | } 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.Sequence.Factory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | public partial class Advisor 6 | { 7 | internal partial class Sequence 8 | { 9 | public partial class Factory 10 | { 11 | private Func[] Sequence; 12 | 13 | public Factory(Func[] sequence) 14 | { 15 | this.Sequence = sequence; 16 | } 17 | 18 | public IAdvice Create() 19 | { 20 | var _sequence = this.Sequence; 21 | var _array = new IAdvice[_sequence.Length]; 22 | for (var _index = 0; _index < _sequence.Length; _index++) { _array[_index] = _sequence[_index](); } 23 | return new Advisor.Sequence(_array); 24 | } 25 | } 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.Sequence.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Threading.Tasks; 4 | 5 | namespace Puresharp 6 | { 7 | public partial class Advisor 8 | { 9 | internal partial class Sequence : IAdvice 10 | { 11 | private IAdvice[] m_Sequence; 12 | 13 | public Sequence(IAdvice[] sequence) 14 | { 15 | this.m_Sequence = sequence; 16 | } 17 | 18 | public void Instance(T value) 19 | { 20 | var _sequence = this.m_Sequence; 21 | for (var _index = 0; _index < _sequence.Length; _index++) { _sequence[_index].Instance(value); } 22 | } 23 | 24 | public void Argument(ref T value) 25 | { 26 | var _sequence = this.m_Sequence; 27 | for (var _index = 0; _index < _sequence.Length; _index++) { _sequence[_index].Argument(ref value); } 28 | } 29 | 30 | public void Begin() 31 | { 32 | var _sequence = this.m_Sequence; 33 | for (var _index = 0; _index < _sequence.Length; _index++) { _sequence[_index].Begin(); } 34 | } 35 | 36 | public void Await(MethodInfo method, Task task) 37 | { 38 | var _sequence = this.m_Sequence; 39 | for (var _index = _sequence.Length - 1; _index >= 0; _index--) { _sequence[_index].Await(method, task); } 40 | } 41 | 42 | public void Await(MethodInfo method, Task task) 43 | { 44 | var _sequence = this.m_Sequence; 45 | for (var _index = _sequence.Length - 1; _index >= 0; _index--) { _sequence[_index].Await(method, task); } 46 | } 47 | 48 | public void Continue() 49 | { 50 | var _sequence = this.m_Sequence; 51 | for (var _index = 0; _index < _sequence.Length; _index++) { _sequence[_index].Continue(); } 52 | } 53 | 54 | public void Throw(ref Exception exception) 55 | { 56 | var _sequence = this.m_Sequence; 57 | for (var _index = _sequence.Length - 1; _index >= 0; _index--) { _sequence[_index].Throw(ref exception); } 58 | } 59 | 60 | public void Throw(ref Exception exception, ref T value) 61 | { 62 | var _sequence = this.m_Sequence; 63 | for (var _index = _sequence.Length - 1; _index >= 0; _index--) { _sequence[_index].Throw(ref exception, ref value); } 64 | } 65 | 66 | public void Return() 67 | { 68 | var _sequence = this.m_Sequence; 69 | for (var _index = _sequence.Length - 1; _index >= 0; _index--) { _sequence[_index].Return(); } 70 | } 71 | 72 | public void Return(ref T value) 73 | { 74 | var _sequence = this.m_Sequence; 75 | for (var _index = _sequence.Length - 1; _index >= 0; _index--) { _sequence[_index].Return(ref value); } 76 | } 77 | 78 | public void Dispose() 79 | { 80 | var _sequence = this.m_Sequence; 81 | for (var _index = _sequence.Length - 1; _index >= 0; _index--) { _sequence[_index].Dispose(); } 82 | } 83 | } 84 | } 85 | } -------------------------------------------------------------------------------- /Puresharp/Puresharp/Advisor/Advisor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel; 3 | using System.Diagnostics; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | 7 | namespace Puresharp 8 | { 9 | /// 10 | /// Advisor used to advise a method. 11 | /// 12 | public partial class Advisor 13 | { 14 | static private Func m_Null = new Func(() => new Advice()); 15 | 16 | /// 17 | /// Null advisor that can create advice doing nothing. 18 | /// 19 | [DebuggerHidden] 20 | [Browsable(false)] 21 | [EditorBrowsable(EditorBrowsableState.Never)] 22 | static public Func Null 23 | { 24 | get { return Advisor.m_Null; } 25 | } 26 | 27 | private Func m_Create; 28 | 29 | internal Advisor(Func create) 30 | { 31 | this.m_Create = create; 32 | } 33 | 34 | internal Func Create 35 | { 36 | get { return this.m_Create; } 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /Puresharp/Puresharp/Aspect/Aspect.Activity.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Reflection; 4 | 5 | namespace Puresharp 6 | { 7 | abstract public partial class Aspect 8 | { 9 | internal class Activity 10 | { 11 | internal readonly Aspect.Activity Authority; 12 | public readonly Type Type; 13 | public readonly MethodBase Method; 14 | public readonly Signature Signature; 15 | internal readonly Func Update; 16 | 17 | public Activity(Type type, MethodBase method) 18 | : this(null, type, method, method.Signature(), _Method => _Method) 19 | { 20 | } 21 | 22 | public Activity(Activity authority, Type type, MethodBase method, Signature signature, Func update) 23 | { 24 | this.Authority = authority; 25 | this.Type = type; 26 | this.Method = method; 27 | this.Signature = signature; 28 | this.Update = update; 29 | } 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /Puresharp/Puresharp/Aspect/Aspect.Addin.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | abstract public partial class Aspect 6 | { 7 | /// 8 | /// Specifies that the aspect should behave as an addin. 9 | /// 10 | [AttributeUsage(AttributeTargets.Class, AllowMultiple=false, Inherited=true)] 11 | public class Addin : Attribute 12 | { 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Aspect/Aspect.Addon.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | abstract public partial class Aspect 6 | { 7 | /// 8 | /// Specifies that the aspect should behave as an addon. 9 | /// 10 | [AttributeUsage(AttributeTargets.Class, AllowMultiple=false, Inherited=true)] 11 | public class Addon : Attribute 12 | { 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Aspect/Aspect.Listener.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace Puresharp 5 | { 6 | abstract public partial class Aspect 7 | { 8 | private class Listener : IDisposable 9 | { 10 | private Aspect m_Aspect; 11 | private Pointcut m_Pointcut; 12 | private IAudition m_Audition; 13 | 14 | public Listener(Aspect aspect, Pointcut pointcut) 15 | { 16 | this.m_Aspect = aspect; 17 | this.m_Pointcut = pointcut; 18 | var _listener = new Listener(_Method => 19 | { 20 | if (this.m_Pointcut.Match(_Method)) 21 | { 22 | aspect.Weave(_Method); 23 | } 24 | }); 25 | this.m_Audition = Metadata.Functions.Accept(_listener); 26 | } 27 | 28 | public void Dispose() 29 | { 30 | this.m_Audition.Dispose(); 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Aspect/Aspect.Monitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace Puresharp 5 | { 6 | abstract public partial class Aspect 7 | { 8 | static public class Monitor 9 | { 10 | static public Collection Aspectization 11 | { 12 | get { return new Collection(Aspect.m_Aspectization.Where(_Aspect => !(_Aspect is Proxy.Manager)).ToArray()); } 13 | } 14 | 15 | static public Collection Weaving 16 | { 17 | get 18 | { 19 | lock (Aspect.Resource) 20 | { 21 | return new Collection(Aspect.m_Aspectization.Where(_Aspect => !(_Aspect is Proxy.Manager)).SelectMany(_Aspect => _Aspect.m_Weaving).ToArray()); 22 | } 23 | } 24 | } 25 | 26 | static public Collection Network 27 | { 28 | get 29 | { 30 | lock (Aspect.Resource) 31 | { 32 | return new Collection(Aspect.m_Aspectization.Where(_Aspect => !(_Aspect is Proxy.Manager)).SelectMany(_Aspect => _Aspect.m_Network).ToArray()); 33 | } 34 | } 35 | } 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /Puresharp/Puresharp/Aspect/Directory/Aspect.Directory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Diagnostics; 5 | using System.Linq; 6 | using System.Reflection; 7 | 8 | namespace Puresharp 9 | { 10 | abstract public partial class Aspect 11 | { 12 | static private partial class Directory 13 | { 14 | static private readonly Dictionary m_Dictionary = new Dictionary(); 15 | 16 | static private Aspect.Directory.Entry Obtain(MethodBase method) 17 | { 18 | var _method = method; 19 | if (_method.DeclaringType != _method.ReflectedType) 20 | { 21 | if (_method is MethodInfo) { _method = (_method as MethodInfo).GetBaseDefinition(); } 22 | _method = _method.DeclaringType.FindMembers(MemberTypes.Method, BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly, (_Method, _Criteria) => _Method is ConstructorInfo || _Method is MethodInfo && (_Method as MethodInfo).GetBaseDefinition() == _method, null).Single() as MethodBase; 23 | } 24 | if (Aspect.Directory.m_Dictionary.TryGetValue(method, out var _entry)) { return _entry; } 25 | _entry = new Aspect.Directory.Entry(_method.DeclaringType, _method, new Aspect.Activity(_method.DeclaringType, _method)); 26 | Aspect.Directory.m_Dictionary.Add(_method, _entry); 27 | return _entry; 28 | } 29 | 30 | static public IEnumerable Index() 31 | { 32 | return Aspect.Directory.m_Dictionary.Values.Where(_Entry => _Entry.Count() > 0).Select(_Entry => _Entry.Method).ToArray(); 33 | } 34 | 35 | static public IEnumerable Index(Aspect aspect) 36 | { 37 | return Aspect.Directory.m_Dictionary.Values.Where(_Entry => _Entry.Contains(aspect)).Select(_Entry => _Entry.Method).ToArray(); 38 | } 39 | 40 | static public IEnumerable Index(MethodBase method) 41 | { 42 | return Aspect.Directory.Obtain(method).ToArray(); 43 | } 44 | 45 | static public void Add(MethodBase method, Aspect aspect) 46 | { 47 | Aspect.Directory.Obtain(method).Add(aspect); 48 | } 49 | 50 | static public void Remove(MethodBase method) 51 | { 52 | var _entry = Aspect.Directory.Obtain(method); 53 | foreach (var _aspect in _entry.ToArray()) { _entry.Remove(_aspect); } 54 | } 55 | 56 | static public void Remove(Aspect aspect) 57 | { 58 | foreach (var _method in Aspect.Directory.Index(aspect).ToArray()) 59 | { 60 | Aspect.Directory.Obtain(_method).Remove(aspect); 61 | } 62 | } 63 | 64 | static public void Remove(MethodBase method, Aspect aspect) 65 | { 66 | Aspect.Directory.Obtain(method).Remove(aspect); 67 | } 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /Puresharp/Puresharp/Aspect/__Aspect.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Reflection; 6 | 7 | namespace Puresharp 8 | { 9 | static internal class __Aspect 10 | { 11 | static public IEnumerable> Manage(this IEnumerable aspectization, MethodBase method) 12 | { 13 | return aspectization.SelectMany(_Aspect => _Aspect.Manage(method).Reverse()).Where(_Advisor => _Advisor != null && _Advisor.Create != null && _Advisor.Create != Advisor.Null).Select(_Advisor => _Advisor.Create); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Attribute/Attribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Reflection; 4 | 5 | namespace Puresharp 6 | { 7 | static internal class Attribute 8 | where T : Attribute 9 | { 10 | static public bool On(MethodBase method) 11 | { 12 | if (method.GetCustomAttributes(Metadata.Type, true).Any()) { return true; } 13 | if (Attribute.On(method.DeclaringType)) { return true; } 14 | foreach (var _property in method.DeclaringType.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) 15 | { 16 | if (_property.GetAccessors().Contains(method)) 17 | { 18 | if (_property.GetCustomAttributes(Metadata.Type, true).Any()) { return true; } 19 | break; 20 | } 21 | } 22 | var _method = method.GetBaseDefinition(); 23 | if (_method != null) 24 | { 25 | if (_method.GetCustomAttributes(Metadata.Type, true).Any()) { return true; } 26 | foreach (var _property in _method.DeclaringType.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) 27 | { 28 | if (_property.GetAccessors().Contains(_method)) 29 | { 30 | if (_property.GetCustomAttributes(Metadata.Type, true).Any()) { return true; } 31 | break; 32 | } 33 | } 34 | } 35 | if (method.GetParameters().Any(_Parameter => _Parameter.GetCustomAttributes(Metadata.Type, true).Any())) { return true; } 36 | foreach (var _map in method.DeclaringType.GetInterfaces().Select(_Interface => method.DeclaringType.GetInterfaceMap(_Interface))) 37 | { 38 | for (var _index = 0; _index < _map.TargetMethods.Length; _index++) 39 | { 40 | if (_map.TargetMethods[_index] == method) 41 | { 42 | if (_map.InterfaceMethods[_index].GetParameters().Any(_Parameter => _Parameter.GetCustomAttributes(Metadata.Type, true).Any())) { return true; } 43 | } 44 | } 45 | } 46 | _method = null; 47 | foreach (var _type in method.DeclaringType.GetInterfaces()) 48 | { 49 | var _map = method.DeclaringType.GetInterfaceMap(_type); 50 | for (var _index = 0; _index < _map.InterfaceMethods.Length; _index++) 51 | { 52 | if (_map.TargetMethods[_index] == method) 53 | { 54 | _method = _map.InterfaceMethods[_index]; 55 | if (_method.GetCustomAttributes(Metadata.Type, true).Any()) { return true; } 56 | foreach (var _property in _method.DeclaringType.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) 57 | { 58 | if (_property.GetAccessors().Contains(_method)) 59 | { 60 | if (_property.GetCustomAttributes(Metadata.Type, true).Any()) { return true; } 61 | break; 62 | } 63 | } 64 | return false; 65 | } 66 | } 67 | } 68 | return false; 69 | } 70 | 71 | static public bool On(MethodBase method, ParameterInfo parameter) 72 | { 73 | foreach (var _map in method.DeclaringType.GetInterfaces().Select(_Interface => method.DeclaringType.GetInterfaceMap(_Interface))) 74 | { 75 | for (var _index = 0; _index < _map.TargetMethods.Length; _index++) 76 | { 77 | if (_map.TargetMethods[_index] == method) 78 | { 79 | if (_map.InterfaceMethods[_index].GetParameters()[parameter.Position].GetCustomAttributes(Metadata.Type, true).Any()) { return true; } 80 | return false; 81 | } 82 | } 83 | } 84 | return parameter.GetCustomAttributes(Metadata.Type, true).Any(); 85 | } 86 | 87 | static public T From(MethodBase method, ParameterInfo parameter) 88 | { 89 | foreach (var _map in method.DeclaringType.GetInterfaces().Select(_Interface => method.DeclaringType.GetInterfaceMap(_Interface))) 90 | { 91 | for (var _index = 0; _index < _map.TargetMethods.Length; _index++) 92 | { 93 | if (_map.TargetMethods[_index] == method) 94 | { 95 | var _attributes = _map.InterfaceMethods[_index].GetParameters()[parameter.Position].GetCustomAttributes(Metadata.Type, true); 96 | if (_attributes.Length > 0) { return _attributes.Single() as T; } 97 | return null; 98 | } 99 | } 100 | } 101 | return parameter.GetCustomAttributes(Metadata.Type, true).Single() as T; 102 | } 103 | 104 | static public bool On(Type type) 105 | { 106 | var _type = type; 107 | while (true) 108 | { 109 | if (_type.GetCustomAttributes(Metadata.Type, true).Any()) { return true; } 110 | _type = _type.BaseType; 111 | if (_type == null) { break; } 112 | } 113 | foreach (var _interface in type.GetInterfaces()) { if (_interface.GetCustomAttributes(Metadata.Type, true).Any()) { return true; } } 114 | return false; 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Audience/IAudition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | public interface IAudition : IDisposable 6 | { 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Audience/IListenable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | public interface IListenable 6 | where T : class 7 | { 8 | IAudition Accept(IListener listener); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Audience/IListener.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | public interface IListener 6 | where T : class 7 | { 8 | void Listen(T item); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Audience/Listener.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | public class Listener : IListener 6 | where T : class 7 | { 8 | private Action m_Action; 9 | 10 | public Listener(Action action) 11 | { 12 | this.m_Action = action; 13 | } 14 | 15 | public void Listen(T item) 16 | { 17 | this.m_Action(item); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/Composition.IVisitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace Puresharp 5 | { 6 | public sealed partial class Composition 7 | { 8 | public interface IVisitor 9 | { 10 | void Visit(ISetup setup) 11 | where T : class; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/Composition.Visitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace Puresharp 5 | { 6 | public sealed partial class Composition 7 | { 8 | internal class Visitor : Composition.IVisitor 9 | { 10 | private Composition.IVisitor m_Visitor; 11 | 12 | public Visitor(Composition.IVisitor visitor) 13 | { 14 | this.m_Visitor = visitor; 15 | } 16 | 17 | public void Visit(ISetup setup) 18 | where T : class 19 | { 20 | if (setup.Activation == null) { return; } 21 | this.m_Visitor.Visit(setup); 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/Composition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Linq.Expressions; 6 | using System.Reflection; 7 | using System.Reflection.Emit; 8 | 9 | namespace Puresharp 10 | { 11 | /// 12 | /// Composition. 13 | /// 14 | public sealed partial class Composition 15 | { 16 | internal const string Assembly = "Puresharp.Composition.Proxy.Assembly, PublicKey=002400000480000094000000060200000024000052534131000400000100010051A76D0BF5695EB709657A23340D31BF2831DBAEF43A4929F442F960875159CCAD93FBC5994577761C35CFFBA0AEF27255D462A61E1D23D45CF06D9C23FABB59CAB1C6FE510C653CD5843CBC911DBABB0E29201DE8C6035CDEDD3ABEEDBC081C5F85E51D84D6CB068DCF8E9682B2AC3FEE59179221C3E1618A8C3275A8EEDECA"; 17 | static private ModuleBuilder m_Module = AppDomain.CurrentDomain.DefineDynamicModule(Composition.Assembly.Substring(0, Composition.Assembly.IndexOf(',')), Composition.Assembly.Substring(Composition.Assembly.IndexOf('=') + 1)); 18 | static internal ModuleBuilder Module { get { return Composition.m_Module; } } 19 | 20 | private Dictionary m_Dictionary; 21 | 22 | /// 23 | /// Create a composition. 24 | /// 25 | public Composition() 26 | { 27 | this.m_Dictionary = new Dictionary(); 28 | } 29 | 30 | public Composition Accept(IVisitor visitor) 31 | { 32 | var _visitor = new Composition.Visitor(visitor); 33 | foreach (var _item in this.m_Dictionary) { _item.Value.Accept(_visitor); } 34 | return this; 35 | } 36 | 37 | /// 38 | /// Try get setup for specific module. 39 | /// 40 | /// 41 | /// Setup 42 | public ISetup Setup() 43 | where T : class 44 | { 45 | if (!Metadata.Type.IsInterface) { throw new NotSupportedException(); } 46 | this.m_Dictionary.TryGetValue(Metadata.Type, out var _value); 47 | return _value as ISetup; 48 | } 49 | 50 | /// 51 | /// Setup specific module by specifying how to create implementation of module. 52 | /// 53 | /// Type of module 54 | /// Way to create implementation 55 | /// Type of instantiation to setup 56 | /// This 57 | public Composition Setup(Expression> activation, Instantiation instantiation) 58 | where T : class 59 | { 60 | if (!Metadata.Type.IsInterface) { throw new NotSupportedException(); } 61 | if (this.m_Dictionary.TryGetValue(Metadata.Type, out var _value)) 62 | { 63 | (_value as ISetup).Activation = activation; 64 | (_value as ISetup).Instantiation = instantiation; 65 | return this; 66 | } 67 | _value = new Setup(activation, instantiation); 68 | this.m_Dictionary.Add(Metadata.Type, _value); 69 | return this; 70 | } 71 | 72 | /// 73 | /// Setup specific multiton module by specifying how to create implementation of module. 74 | /// 75 | /// Type of module 76 | /// Way to create implementation 77 | /// This 78 | public Composition Setup(Expression> activation) 79 | where T : class 80 | { 81 | return this.Setup(activation, Instantiation.Multiton); 82 | } 83 | 84 | /// 85 | /// Include a fallback composition. 86 | /// 87 | /// Fallback 88 | /// Composition 89 | public Composition Then(Composition composition) 90 | { 91 | if (composition == null || composition == this) { return this; } 92 | composition.Accept(new Fallback(this, composition)); 93 | return this; 94 | } 95 | 96 | /// 97 | /// Create a container based on this composition. 98 | /// 99 | /// Container 100 | public IContainer Materialize() 101 | { 102 | return new Container(this); 103 | } 104 | 105 | private class Fallback : Composition.IVisitor 106 | { 107 | static private Expression> Combine(Expression> authorithy, Expression> auxiliary) 108 | where T : class 109 | { 110 | var _instance = Expression.Parameter(Metadata.Type); 111 | return Expression.Lambda> 112 | ( 113 | Expression.Block 114 | ( 115 | new ParameterExpression[] { _instance }, 116 | Expression.Assign(_instance, authorithy.Body), 117 | Expression.IfThen 118 | ( 119 | Expression.Call 120 | ( 121 | Metadata.Method(() => object.ReferenceEquals(Metadata.Value, Metadata.Value)), 122 | Expression.TypeAs(_instance, Metadata.Type), 123 | Expression.Constant(null, Metadata.Type)) 124 | , 125 | Expression.Assign(_instance, auxiliary.Body) 126 | ), 127 | _instance 128 | ) 129 | ); 130 | } 131 | 132 | private Composition m_Authority; 133 | private Composition m_Auxiliary; 134 | 135 | public Fallback(Composition authorithy, Composition auxiliary) 136 | { 137 | this.m_Authority = authorithy; 138 | this.m_Auxiliary = auxiliary; 139 | } 140 | 141 | public void Visit(ISetup setup) 142 | where T : class 143 | { 144 | var _setup = this.m_Authority.Setup(); 145 | this.m_Authority.Setup(_setup == null ? setup.Activation : Fallback.Combine(_setup.Activation, setup.Activation), setup.Instantiation); 146 | } 147 | } 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/Container.Converter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | using System.Reflection; 4 | 5 | namespace Puresharp 6 | { 7 | internal partial class Container 8 | { 9 | private class Converter : ExpressionVisitor 10 | { 11 | static private Type m_Type = typeof(Metadata<>); 12 | static private string m_Name = nameof(Metadata.Value); 13 | static private MethodInfo m_Method = Metadata.Method(_Resolver => _Resolver.Resolve()).GetGenericMethodDefinition(); 14 | private Expression m_Resolver; 15 | 16 | public Converter(Expression resolver) 17 | { 18 | this.m_Resolver = resolver; 19 | } 20 | 21 | override protected Expression VisitMember(MemberExpression node) 22 | { 23 | var _member = node.Member; 24 | if (_member is FieldInfo && _member.DeclaringType.IsGenericType && _member.DeclaringType == Converter.m_Type.MakeGenericType(node.Type) && _member.Name == Converter.m_Name) { return Expression.Call(this.m_Resolver, Converter.m_Method.MakeGenericMethod(node.Type)); } 25 | return base.VisitMember(node); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/Container.Lazy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | internal partial class Container 6 | { 7 | private class Lazy 8 | { 9 | static public Func On(Func activate) 10 | { 11 | var _lazy = new Lazy(activate); 12 | return new Func(_lazy.Value); 13 | } 14 | 15 | private Func m_Activate; 16 | 17 | public Lazy(Func activate) 18 | { 19 | var _handle = new object(); 20 | var _value = null as object; 21 | var _activate = activate; 22 | var _return = new Func((_Resolver, _Reservation) => _value); 23 | this.m_Activate = new Func((_Resolver, _Reservation) => 24 | { 25 | lock (_handle) 26 | { 27 | if (_activate == null) { return _value; } 28 | _value = _activate(_Resolver, _Reservation); 29 | _activate = null; 30 | this.m_Activate = _return; 31 | return _value; 32 | } 33 | }); 34 | } 35 | 36 | public object Value(Resolver resolver, Reservation reservation) 37 | { 38 | return this.m_Activate(resolver, reservation); 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/Container.Map.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace Puresharp 5 | { 6 | internal partial class Container 7 | { 8 | internal class Map 9 | { 10 | private Type m_Type; 11 | private Func m_Activate; 12 | private LambdaExpression m_Activation; 13 | private Instantiation m_Instantiation; 14 | 15 | public Map(Type type, Func activate, LambdaExpression activation, Instantiation instantiation) 16 | { 17 | this.m_Type = type; 18 | this.m_Activate = activate; 19 | this.m_Activation = activation; 20 | this.m_Instantiation = instantiation; 21 | } 22 | 23 | public Type Type 24 | { 25 | get { return this.m_Type; } 26 | } 27 | 28 | public Func Activate 29 | { 30 | get { return this.m_Activate; } 31 | } 32 | 33 | public LambdaExpression Activation 34 | { 35 | get { return this.m_Activation; } 36 | } 37 | 38 | public Instantiation Instantiation 39 | { 40 | get { return this.m_Instantiation; } 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/Container.Mapping.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq.Expressions; 5 | 6 | namespace Puresharp 7 | { 8 | internal partial class Container 9 | { 10 | private class Mapping : Composition.IVisitor, IEnumerable, IDisposable 11 | { 12 | private Composition m_Composition; 13 | private LinkedList m_Value = new LinkedList(); 14 | 15 | public Mapping(Composition composition) 16 | { 17 | this.m_Composition = composition; 18 | this.m_Composition.Accept(this); 19 | } 20 | 21 | void Composition.IVisitor.Visit(ISetup setup) 22 | { 23 | var _body = Expression.Convert(new Converter(Parameter.Expression).Visit(setup.Activation.Body), Metadata.Type); 24 | var _activate = Expression.Lambda>(_body, Parameter.Expression, Parameter.Expression).Compile(); 25 | this.m_Value.AddLast(new Map(Metadata.Type, Proxy.Create(_activate), setup.Activation, setup.Instantiation)); 26 | } 27 | 28 | public void Dispose() 29 | { 30 | this.m_Value = null; 31 | this.m_Composition = null; 32 | } 33 | 34 | IEnumerator IEnumerable.GetEnumerator() 35 | { 36 | return this.m_Value.GetEnumerator(); 37 | } 38 | 39 | IEnumerator IEnumerable.GetEnumerator() 40 | { 41 | return this.m_Value.GetEnumerator(); 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/Container.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Linq.Expressions; 6 | 7 | namespace Puresharp 8 | { 9 | /// 10 | /// Container. 11 | /// 12 | internal partial class Container : IContainer 13 | { 14 | private Dictionary m_Mapping = new Dictionary(); 15 | private Dictionary>> m_Dictionary = new Dictionary>>(); 16 | private Reservation m_Reservation; 17 | 18 | /// 19 | /// Create a container based on composition. 20 | /// 21 | /// Composition 22 | public Container(Composition composition) 23 | { 24 | using (var _mapping = new Mapping(composition)) 25 | { 26 | var _dictionary = new Dictionary>>(); 27 | var _reservation = new Reservation(); 28 | foreach (var _map in _mapping) 29 | { 30 | var _type = _map.Type; 31 | var _activate = _map.Activate; 32 | var _instantiation = _map.Instantiation; 33 | switch (_map.Instantiation) 34 | { 35 | case Instantiation.Volatile: 36 | { 37 | this.m_Mapping.Add(_type, _map); 38 | _dictionary.Add(_type, new Func>(() => new Func((_Resolver, _Reservation) => 39 | { 40 | var _value = _activate(_Resolver, _Reservation); 41 | _Reservation.Add(_value); 42 | return _value; 43 | }))); 44 | break; 45 | } 46 | case Instantiation.Multiton: 47 | { 48 | this.m_Mapping.Add(_type, _map); 49 | _dictionary.Add(_type, new Func>(() => 50 | { 51 | var _lazy = new Lazy((_Resolver, _Reservation) => 52 | { 53 | var _value = _activate(_Resolver, _Reservation); 54 | _Reservation.Add(_value); 55 | return _value; 56 | }); 57 | return new Func(_lazy.Value); 58 | })); 59 | break; 60 | } 61 | case Instantiation.Singleton: 62 | { 63 | this.m_Mapping.Add(_type, _map); 64 | var _lazy = new Lazy((_Resolver, _Reservation) => 65 | { 66 | var _value = _activate(_Resolver, _Reservation); 67 | _reservation.Add(_value); 68 | return _value; 69 | }); 70 | _dictionary.Add(_type, new Func>(() => _lazy.Value)); 71 | break; 72 | } 73 | } 74 | } 75 | this.m_Dictionary = _dictionary; 76 | this.m_Reservation = _reservation; 77 | } 78 | } 79 | 80 | /// 81 | /// Instantiate a module. 82 | /// 83 | /// Type of module 84 | /// Module 85 | public IModule Module() 86 | where T : class 87 | { 88 | var _map = this.m_Mapping[Metadata.Type]; 89 | var _dictionary = new Dictionary>(); 90 | foreach (var _item in this.m_Dictionary) { _dictionary.Add(_item.Key, _item.Value()); } 91 | return new Module(_map.Activation as Expression>, _map.Instantiation, new Resolver(_dictionary)); 92 | } 93 | 94 | /// 95 | /// Dispose container. 96 | /// 97 | public void Dispose() 98 | { 99 | this.m_Reservation.Dispose(); 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/IContainer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | public interface IContainer : IDisposable 6 | { 7 | IModule Module() 8 | where T : class; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/IModule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | public interface IModule : IDisposable 6 | where T : class 7 | { 8 | T Value { get; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/IResolver.cs: -------------------------------------------------------------------------------- 1 | //using System; 2 | 3 | //namespace Puresharp 4 | //{ 5 | // public interface IResolver : IDisposable 6 | // { 7 | // T Resolve() 8 | // where T : class; 9 | // } 10 | //} 11 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/ISetup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace Puresharp 5 | { 6 | public interface ISetup 7 | { 8 | Expression> Activation { get; set; } 9 | Instantiation Instantiation { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/Module.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Linq.Expressions; 4 | 5 | namespace Puresharp 6 | { 7 | internal class Module : IModule 8 | where T : class 9 | { 10 | [DebuggerBrowsable(DebuggerBrowsableState.Never)] 11 | private Expression> m_Activation; 12 | 13 | [DebuggerBrowsable(DebuggerBrowsableState.Never)] 14 | private Instantiation m_Instantiation; 15 | 16 | [DebuggerBrowsable(DebuggerBrowsableState.Never)] 17 | private Resolver m_Resolver; 18 | 19 | [DebuggerBrowsable(DebuggerBrowsableState.Never)] 20 | private bool m_Activated; 21 | 22 | public Module(Expression> activation, Instantiation instantiation, Resolver resolver) 23 | { 24 | this.m_Activation = activation; 25 | this.m_Instantiation = instantiation; 26 | this.m_Resolver = resolver; 27 | this.m_Activated = false; 28 | } 29 | 30 | public Expression> Activation 31 | { 32 | get { return this.m_Activation; } 33 | } 34 | 35 | public Instantiation Instantiation 36 | { 37 | get { return this.m_Instantiation; } 38 | } 39 | 40 | [DebuggerBrowsable(DebuggerBrowsableState.Never)] 41 | public bool Activated 42 | { 43 | get { return this.m_Activated; } 44 | } 45 | 46 | public T Value 47 | { 48 | get 49 | { 50 | this.m_Activated = true; 51 | return this.m_Resolver.Resolve(); 52 | } 53 | } 54 | 55 | public void Dispose() 56 | { 57 | this.m_Resolver.Dispose(); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/Reservation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Puresharp 5 | { 6 | internal class Reservation : IDisposable 7 | { 8 | private LinkedList m_Reserve = new LinkedList(); 9 | 10 | public void Add(object item) 11 | { 12 | if (item is IDisposable) 13 | { 14 | this.m_Reserve.AddLast(item as IDisposable); 15 | } 16 | } 17 | 18 | public void Dispose() 19 | { 20 | foreach (var _item in this.m_Reserve) { _item.Dispose(); } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/Resolver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Puresharp 5 | { 6 | internal class Resolver 7 | { 8 | private Dictionary> m_Dictionary; 9 | private Reservation m_Reservation; 10 | 11 | public Resolver(Dictionary> dictionary) 12 | { 13 | this.m_Dictionary = dictionary; 14 | this.m_Reservation = new Reservation(); 15 | } 16 | 17 | public T Resolve() 18 | where T : class 19 | { 20 | return this.m_Dictionary[Metadata.Type](this, this.m_Reservation) as T; 21 | } 22 | 23 | public void Dispose() 24 | { 25 | this.m_Reservation.Dispose(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Composition/Setup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace Puresharp 5 | { 6 | internal abstract class Setup 7 | { 8 | abstract public void Accept(Composition.IVisitor visitor); 9 | } 10 | 11 | internal class Setup : Setup, ISetup 12 | where T : class 13 | { 14 | private Expression> m_Activation; 15 | private Instantiation m_Instantiation; 16 | 17 | public Setup(Expression> activation, Instantiation instantiation) 18 | { 19 | this.m_Activation = activation; 20 | this.m_Instantiation = instantiation; 21 | } 22 | 23 | public Expression> Activation 24 | { 25 | get { return this.m_Activation; } 26 | set { this.m_Activation = value; } 27 | } 28 | 29 | public Instantiation Instantiation 30 | { 31 | get { return this.m_Instantiation; } 32 | set { this.m_Instantiation = value; } 33 | } 34 | 35 | public override void Accept(Composition.IVisitor visitor) 36 | { 37 | visitor.Visit(this); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Concurrency.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | internal enum Concurrency 6 | { 7 | None = 0, 8 | Locked = 1, 9 | Interlocked = 2 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Data/Data.Collection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Diagnostics; 5 | using System.Linq; 6 | 7 | namespace Puresharp 8 | { 9 | static public partial class Data 10 | { 11 | internal sealed partial class Collection : IEnumerable 12 | { 13 | static public implicit operator T[] (Data.Collection collection) 14 | { 15 | var _list = collection.m_List; 16 | var _length = _list.Count; 17 | var _array = new T[_length]; 18 | for (var _index = 0; _index < _length; _index++) { _array[_index] = _list[_index]; } 19 | return _array; 20 | } 21 | 22 | [DebuggerBrowsable(DebuggerBrowsableState.Never)] 23 | static private T[] m_Empty = new T[0]; 24 | 25 | [DebuggerBrowsable(DebuggerBrowsableState.Never)] 26 | private IList m_List; 27 | 28 | public Collection() 29 | : this(Data.Collection.m_Empty) 30 | { 31 | } 32 | 33 | public Collection(IEnumerable enumerable) 34 | : this(enumerable is IList ? enumerable as IList : new Enumerable(enumerable)) 35 | { 36 | } 37 | 38 | public Collection(IList list) 39 | { 40 | this.m_List = list; 41 | } 42 | 43 | public Collection(params T[] array) 44 | : this(array as IList) 45 | { 46 | } 47 | 48 | public Collection(Data.Collection collection) 49 | : this(collection.m_List) 50 | { 51 | } 52 | 53 | public T this[int index] 54 | { 55 | get { return this.m_List[index]; } 56 | } 57 | 58 | public int Count 59 | { 60 | get { return this.m_List.Count; } 61 | } 62 | 63 | public int Index(T value) 64 | { 65 | return this.m_List.IndexOf(value); 66 | } 67 | 68 | public IEnumerator Enumerator() 69 | { 70 | return this.m_List.GetEnumerator(); 71 | } 72 | 73 | IEnumerator IEnumerable.GetEnumerator() 74 | { 75 | return this.m_List.GetEnumerator(); 76 | } 77 | 78 | IEnumerator IEnumerable.GetEnumerator() 79 | { 80 | return this.m_List.GetEnumerator(); 81 | } 82 | } 83 | 84 | [DebuggerDisplay("Count = {this.Count, nq}")] 85 | [DebuggerTypeProxy(typeof(Data.Collection<>.Debugger))] 86 | internal sealed partial class Collection 87 | { 88 | private sealed class Debugger 89 | { 90 | [DebuggerBrowsable(DebuggerBrowsableState.Never)] 91 | private Data.Collection m_Collection; 92 | 93 | [DebuggerBrowsable(DebuggerBrowsableState.Never)] 94 | private T[] m_View; 95 | 96 | public Debugger(Data.Collection collection) 97 | { 98 | this.m_Collection = collection; 99 | } 100 | 101 | [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] 102 | public T[] View 103 | { 104 | get 105 | { 106 | if (this.m_View == null) { this.m_View = this.m_Collection.ToArray(); } 107 | return this.m_View; 108 | } 109 | } 110 | } 111 | } 112 | 113 | internal partial class Collection : IEnumerable> 114 | { 115 | static public implicit operator Data.Map(Data.Collection collection) 116 | { 117 | return collection == null ? null : collection.m_Map; 118 | } 119 | 120 | static private IDictionary m_Empty = new Dictionary(); 121 | 122 | private IDictionary m_Dictionary; 123 | private Data.Map m_Map; 124 | 125 | public Collection() 126 | : this(Data.Collection.m_Empty) 127 | { 128 | } 129 | 130 | public Collection(IDictionary dictionary) 131 | { 132 | this.m_Dictionary = dictionary; 133 | this.m_Map = new Data.Map(dictionary); 134 | } 135 | 136 | public TValue this[TKey key] 137 | { 138 | get { return this.m_Dictionary[key]; } 139 | } 140 | 141 | public bool Contains(TKey key) 142 | { 143 | return this.m_Dictionary.ContainsKey(key); 144 | } 145 | 146 | public int Count 147 | { 148 | get { return this.m_Dictionary.Count; } 149 | } 150 | 151 | public IEnumerator> Enumerator() 152 | { 153 | return this.m_Dictionary.GetEnumerator(); 154 | } 155 | 156 | IEnumerator> IEnumerable>.GetEnumerator() 157 | { 158 | return this.m_Dictionary.GetEnumerator(); 159 | } 160 | 161 | IEnumerator IEnumerable.GetEnumerator() 162 | { 163 | return this.m_Dictionary.GetEnumerator(); 164 | } 165 | } 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Data/Data.Enumerable.Enumerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace Puresharp 6 | { 7 | static public partial class Data 8 | { 9 | internal sealed partial class Enumerable 10 | { 11 | private sealed class Enumerator : IEnumerator 12 | { 13 | private Data.Enumerable m_Enumerable; 14 | private IList m_Collection; 15 | private int m_Index; 16 | 17 | public Enumerator(Data.Enumerable enumerable) 18 | { 19 | this.m_Enumerable = enumerable; 20 | this.m_Collection = this.m_Enumerable.m_List; 21 | this.m_Index = -1; 22 | } 23 | 24 | public T Current 25 | { 26 | get { return this.m_Collection[this.m_Index]; } 27 | } 28 | 29 | object IEnumerator.Current 30 | { 31 | get { return this.m_Collection[this.m_Index]; } 32 | } 33 | 34 | public bool MoveNext() 35 | { 36 | if (this.m_Index < this.m_Collection.Count - 1) 37 | { 38 | this.m_Index++; 39 | return true; 40 | } 41 | if (this.m_Enumerable.m_Activated) { return false; } 42 | if (this.m_Enumerable.m_Enumerator.MoveNext()) 43 | { 44 | this.m_Index++; 45 | this.m_Collection.Add(this.m_Enumerable.m_Enumerator.Current); 46 | return true; 47 | } 48 | this.m_Enumerable.m_Enumerator.Dispose(); 49 | this.m_Enumerable.m_Activated = true; 50 | return false; 51 | } 52 | 53 | public void Reset() 54 | { 55 | this.m_Index = -1; 56 | } 57 | 58 | public void Dispose() 59 | { 60 | } 61 | } 62 | } 63 | } 64 | } -------------------------------------------------------------------------------- /Puresharp/Puresharp/Data/Data.Enumerable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace Puresharp 7 | { 8 | static public partial class Data 9 | { 10 | internal sealed partial class Enumerable : IList 11 | { 12 | static public implicit operator T[] (Data.Enumerable enumerable) 13 | { 14 | var _list = enumerable.m_List; 15 | var _length = enumerable.Count; 16 | var _array = new T[_length]; 17 | for (var _index = 0; _index < _length; _index++) { _array[_index] = _list[_index]; } 18 | return _array; 19 | } 20 | 21 | private readonly IEnumerable m_Enumerable; 22 | private readonly IList m_List; 23 | private readonly IEnumerator m_Enumerator; 24 | private bool m_Activated; 25 | 26 | public Enumerable(IEnumerable enumerable) 27 | { 28 | this.m_Enumerable = enumerable; 29 | if (enumerable is IList) 30 | { 31 | this.m_Activated = true; 32 | this.m_List = enumerable as IList; 33 | } 34 | else 35 | { 36 | this.m_Enumerator = this.m_Enumerable.GetEnumerator(); 37 | this.m_List = new List(); 38 | } 39 | } 40 | 41 | public T this[int index] 42 | { 43 | get 44 | { 45 | if (this.m_Activated) { return this.m_List[index]; } 46 | var _enumerator = new Enumerable.Enumerator(this); 47 | var _index = 0; 48 | while (_enumerator.MoveNext()) 49 | { 50 | _index++; 51 | if (_index == index) { return _enumerator.Current; } 52 | } 53 | throw new IndexOutOfRangeException(); 54 | } 55 | set { throw new NotImplementedException(); } 56 | } 57 | 58 | public int Count 59 | { 60 | get 61 | { 62 | if (this.m_Activated) { return this.m_List.Count; } 63 | var _enumerator = new Enumerable.Enumerator(this); 64 | while (_enumerator.MoveNext()) { } 65 | return this.m_List.Count; 66 | } 67 | } 68 | 69 | public bool Contains(T item) 70 | { 71 | if (this.m_Activated) { return this.m_List.Contains(item); } 72 | var _enumerator = new Enumerable.Enumerator(this); 73 | while (_enumerator.MoveNext()) { if (object.Equals(_enumerator.Current, item)) { return true; } } 74 | return false; 75 | } 76 | 77 | public void CopyTo(T[] buffer, int index) 78 | { 79 | if (this.m_Activated) { this.m_List.CopyTo(buffer, index); } 80 | else 81 | { 82 | var _enumerator = new Enumerable.Enumerator(this); 83 | var _index = index; 84 | while (_enumerator.MoveNext()) { buffer[_index++] = _enumerator.Current; } 85 | } 86 | } 87 | 88 | public bool IsReadOnly 89 | { 90 | get { return true; } 91 | } 92 | 93 | public IEnumerator GetEnumerator() 94 | { 95 | if (this.m_Activated) 96 | { 97 | if (this.m_List == null) { return this.m_Enumerable.GetEnumerator(); } 98 | else { return this.m_List.GetEnumerator(); } 99 | } 100 | return new Enumerable.Enumerator(this); 101 | } 102 | 103 | IEnumerator IEnumerable.GetEnumerator() 104 | { 105 | if (this.m_Activated) 106 | { 107 | if (this.m_List == null) { return this.m_Enumerable.GetEnumerator(); } 108 | else { return this.m_List.GetEnumerator(); } 109 | } 110 | return new Enumerable.Enumerator(this); 111 | } 112 | 113 | public int IndexOf(T item) 114 | { 115 | if (this.m_Activated) 116 | { 117 | if (this.m_List == null) 118 | { 119 | using (var _enumerator = this.m_Enumerable.GetEnumerator()) 120 | { 121 | var _index = 0; 122 | while (_enumerator.MoveNext()) 123 | { 124 | if (object.Equals(_enumerator.Current, item)) { return _index; } 125 | _index++; 126 | } 127 | } 128 | return -1; 129 | } 130 | else { return this.m_List.IndexOf(item); } 131 | } 132 | using (var _enumerator = new Enumerable.Enumerator(this)) 133 | { 134 | var _index = 0; 135 | while (_enumerator.MoveNext()) 136 | { 137 | if (object.Equals(_enumerator.Current, item)) { return _index; } 138 | _index++; 139 | } 140 | } 141 | return -1; 142 | } 143 | 144 | public void Insert(int index, T item) 145 | { 146 | throw new NotSupportedException(); 147 | } 148 | 149 | public void RemoveAt(int index) 150 | { 151 | throw new NotSupportedException(); 152 | } 153 | 154 | public void Add(T item) 155 | { 156 | throw new NotSupportedException(); 157 | } 158 | 159 | public void Clear() 160 | { 161 | throw new NotSupportedException(); 162 | } 163 | 164 | public bool Remove(T item) 165 | { 166 | throw new NotSupportedException(); 167 | } 168 | } 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Data/Data.IStorage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace Puresharp 5 | { 6 | static public partial class Data 7 | { 8 | public interface IStorage 9 | { 10 | IQueryable Query() 11 | where T : class; 12 | 13 | void Add(T item) 14 | where T : class; 15 | 16 | void Remove(T item) 17 | where T : class; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Data/Data.IStore.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | static public partial class Data 6 | { 7 | public interface IStore 8 | where T : class 9 | { 10 | T this[string name] { get; set; } 11 | void Add(string name, T value); 12 | void Remove(string name); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Data/Data.Map.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading; 4 | 5 | namespace Puresharp 6 | { 7 | static public partial class Data 8 | { 9 | internal partial class Map 10 | { 11 | private IDictionary m_Dictionary; 12 | private Func m_Lookup; 13 | 14 | public Map(IDictionary dictionary) 15 | { 16 | this.m_Dictionary = dictionary; 17 | this.m_Lookup = new Func(_Key => dictionary[_Key]); 18 | } 19 | 20 | public Map(Func lookup, IDictionary dictionary) 21 | { 22 | this.m_Dictionary = dictionary; 23 | this.m_Lookup = new Func(_Key => 24 | { 25 | if (this.m_Dictionary.TryGetValue(_Key, out var _value)) { return _value; } 26 | _value = lookup(_Key); 27 | this.m_Dictionary.Add(_Key, _value); 28 | return _value; 29 | }); 30 | } 31 | 32 | public Map(Func lookup, Concurrency concurrency = Concurrency.None) 33 | { 34 | this.m_Dictionary = new Dictionary(); 35 | switch (concurrency) 36 | { 37 | case Concurrency.None: 38 | this.m_Lookup = new Func(_Key => 39 | { 40 | TValue _value; 41 | if (this.m_Dictionary.TryGetValue(_Key, out _value)) { return _value; } 42 | _value = lookup(_Key); 43 | this.m_Dictionary.Add(_Key, _value); 44 | return _value; 45 | }); 46 | break; 47 | case Concurrency.Locked: 48 | var _handle = new object(); 49 | this.m_Lookup = new Func(_Key => 50 | { 51 | lock (_handle) 52 | { 53 | TValue _value; 54 | if (this.m_Dictionary.TryGetValue(_Key, out _value)) { return _value; } 55 | _value = lookup(_Key); 56 | this.m_Dictionary.Add(_Key, _value); 57 | return _value; 58 | } 59 | }); 60 | break; 61 | case Concurrency.Interlocked: 62 | this.m_Lookup = new Func(_Key => 63 | { 64 | while (true) 65 | { 66 | TValue _value; 67 | var _dictionary = this.m_Dictionary; 68 | if (_dictionary.TryGetValue(_Key, out _value)) { return _value; } 69 | _value = lookup(_Key); 70 | var _substitution = new Dictionary(_dictionary); 71 | _substitution.Add(_Key, _value); 72 | if (object.ReferenceEquals(Interlocked.CompareExchange(ref this.m_Dictionary, _substitution, _dictionary), _dictionary)) { return _value; } 73 | } 74 | }); 75 | break; 76 | default: throw new NotSupportedException(); 77 | } 78 | } 79 | 80 | public TValue this[TKey key] 81 | { 82 | get { return this.m_Lookup(key); } 83 | } 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Data/Data.Store.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Puresharp 5 | { 6 | static public partial class Data 7 | { 8 | public class Store : IStore 9 | where T : class 10 | { 11 | private Dictionary m_Dictionary; 12 | 13 | public Store() 14 | { 15 | this.m_Dictionary = new Dictionary(); 16 | } 17 | 18 | public T this[string name] 19 | { 20 | get 21 | { 22 | this.m_Dictionary.TryGetValue(name, out var _value); 23 | return _value; 24 | } 25 | set { this.m_Dictionary[name] = value; } 26 | } 27 | 28 | public void Add(string name, T value) 29 | { 30 | this.m_Dictionary.Add(name, value); 31 | } 32 | 33 | public void Remove(string name) 34 | { 35 | this.m_Dictionary.Remove(name); 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Data/Data.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | static public partial class Data 6 | { 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Directory/Directory.Audition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace Puresharp 7 | { 8 | public partial class Directory 9 | { 10 | private class Audition : IAudition 11 | { 12 | private Directory m_Directory; 13 | private IListener m_Listener; 14 | 15 | public Audition(Directory directory, IListener listener) 16 | { 17 | this.m_Directory = directory; 18 | this.m_Listener = listener; 19 | } 20 | 21 | public void Dispose() 22 | { 23 | this.m_Directory.m_Audience.Remove(this.m_Listener); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Directory/Directory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace Puresharp 7 | { 8 | public partial class Directory : IDirectory 9 | where T : class 10 | { 11 | private object m_Handle; 12 | private LinkedList m_Archive; 13 | private List> m_Audience; 14 | 15 | public Directory() 16 | { 17 | this.m_Handle = new object(); 18 | this.m_Archive = new LinkedList(); 19 | this.m_Audience = new List>(); 20 | } 21 | 22 | public void Add(T item) 23 | { 24 | lock (this.m_Handle) 25 | { 26 | this.m_Archive.AddLast(item); 27 | foreach (var _listener in this.m_Audience) { _listener.Listen(item); } 28 | } 29 | } 30 | 31 | public void Remove(T item) 32 | { 33 | lock (this.m_Handle) 34 | { 35 | this.m_Archive.Remove(item); 36 | } 37 | } 38 | 39 | public void Accept(IVisitor visitor) 40 | { 41 | lock (this.m_Handle) 42 | { 43 | foreach (var _item in this.m_Archive.ToArray()) { visitor.Visit(() => _item); } 44 | } 45 | } 46 | 47 | public IAudition Accept(IListener listener) 48 | { 49 | lock (this.m_Handle) 50 | { 51 | this.m_Audience.Add(listener); 52 | foreach (var _item in this.m_Archive.ToArray()) { listener.Listen(_item); } 53 | return new Audition(this, listener); 54 | } 55 | } 56 | 57 | IEnumerator IEnumerable.GetEnumerator() 58 | { 59 | return (this.m_Archive.ToArray() as IEnumerable).GetEnumerator(); 60 | } 61 | 62 | IEnumerator IEnumerable.GetEnumerator() 63 | { 64 | return (this.m_Archive.ToArray() as IEnumerable).GetEnumerator(); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Directory/IDirectory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace Puresharp 6 | { 7 | public interface IDirectory : IEnumerable, IVisitable, IListenable 8 | where T : class 9 | { 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Extension.cs: -------------------------------------------------------------------------------- 1 | //using System; 2 | //using System.Collections; 3 | //using System.Collections.Generic; 4 | //using System.Reflection; 5 | 6 | //namespace Puresharp 7 | //{ 8 | // abstract public class Extension 9 | // { 10 | // abstract public IEnumerable Advise(MethodBase method); 11 | // } 12 | //} -------------------------------------------------------------------------------- /Puresharp/Puresharp/IFormatter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | 4 | namespace Puresharp 5 | { 6 | public interface IFormatter 7 | { 8 | void Serialize(Stream stream, T value); 9 | T Deserialize(Stream value); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Instantiation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | /// 6 | /// Defines the instantiation strategy of an object. 7 | /// 8 | public enum Instantiation 9 | { 10 | /// 11 | /// Always istantiate a new instance. 12 | /// 13 | Volatile, 14 | 15 | /// 16 | /// Instantiate a new instance for each container. 17 | /// 18 | Multiton, 19 | 20 | /// 21 | /// Unique instance for whole composition. 22 | /// 23 | Singleton 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Pointcut/Pointcut.And.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | 5 | namespace Puresharp 6 | { 7 | abstract public partial class Pointcut 8 | { 9 | public class And : Pointcut 10 | where T1 : Pointcut, new() 11 | where T2 : Pointcut, new() 12 | { 13 | sealed override public bool Match(MethodBase method) 14 | { 15 | return Singleton.Value.Match(method) && Singleton.Value.Match(method); 16 | } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Pointcut/Pointcut.Not.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | 5 | namespace Puresharp 6 | { 7 | abstract public partial class Pointcut 8 | { 9 | public class Not : Pointcut 10 | where T : Pointcut, new() 11 | { 12 | sealed override public bool Match(MethodBase method) 13 | { 14 | return !Singleton.Value.Match(method); 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Pointcut/Pointcut.Or.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | 5 | namespace Puresharp 6 | { 7 | abstract public partial class Pointcut 8 | { 9 | public class Or : Pointcut 10 | where T1 : Pointcut, new() 11 | where T2 : Pointcut, new() 12 | { 13 | sealed override public bool Match(MethodBase method) 14 | { 15 | return Singleton.Value.Match(method) || Singleton.Value.Match(method); 16 | } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime; 3 | using System.Runtime.CompilerServices; 4 | using Puresharp; 5 | 6 | [assembly: InternalsVisibleTo(Composition.Assembly)] 7 | [assembly: InternalsVisibleTo(Aspect.Assembly)] -------------------------------------------------------------------------------- /Puresharp/Puresharp/Proxy/Override.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace Puresharp 5 | { 6 | public class Override 7 | where T : class 8 | { 9 | private Func m_Predicate; 10 | private Func> m_Overrider; 11 | 12 | public Override(Func predicate, Func> overrider) 13 | { 14 | this.m_Predicate = predicate; 15 | this.m_Overrider = overrider; 16 | } 17 | 18 | public Func Predicate 19 | { 20 | get { return this.m_Predicate; } 21 | } 22 | 23 | public Func> Overrider 24 | { 25 | get { return this.m_Overrider; } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Puresharp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net452;netcoreapp2.1; 5 | true 6 | Puresharp.snk 7 | 5.0.5 8 | Tony THONG 9 | Tony THONG 10 | 5.0.5 11 | 5.0.5 12 | Tony THONG 13 | 14 | 15 | 16 | Off 17 | C:\Users\THONGTO\source\repos\Puresharp\Puresharp\Puresharp.xml 18 | 19 | 20 | 21 | Off 22 | C:\Users\THONGTO\source\repos\Puresharp\Puresharp\Puresharp.xml 23 | none 24 | false 25 | 26 | 27 | 28 | 29 | 2.2.0 30 | 31 | 32 | 2.2.0 33 | 34 | 35 | 2.2.0 36 | 37 | 38 | 39 | 40 | 41 | ..\..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Web.dll 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Puresharp.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Puresharper/Puresharp/c08233453a2732bb8835175ceeaae243e5e5d72f/Puresharp/Puresharp/Puresharp.snk -------------------------------------------------------------------------------- /Puresharp/Puresharp/Runtime/Declaration.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace Puresharp 5 | { 6 | public static class Declaration 7 | { 8 | public static readonly string Value = Declaration.Evaluate(); 9 | 10 | static private string Evaluate() 11 | { 12 | var type = Metadata.Type; 13 | if (type.IsGenericType) 14 | { 15 | var _Field = Metadata.Field(() => Declaration.Value).Name; 16 | return type.FullName.Remove(type.FullName.IndexOf('`')) + "<" + string.Join(", ", type.GetGenericArguments().Select(_Argument => typeof(Declaration<>).MakeGenericType(new Type[] { _Argument }).GetField(_Field).GetValue(null) as string)) + ">"; 17 | } 18 | return type.FullName; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Runtime/Definition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace Puresharp 5 | { 6 | public static class Definition 7 | { 8 | public static readonly string Value = Definition.Compile(); 9 | 10 | private static string Compile() 11 | { 12 | var _type = Metadata.Type; 13 | if (_type.IsGenericType) 14 | { 15 | var _field = Metadata.Field(() => Declaration.Value).Name; 16 | return string.Concat(new string[] { _type.FullName.Substring(0, _type.FullName.IndexOf('[')), "[", string.Join(", ", _type.GetGenericArguments().Select((Type _Argument) => "[" + (typeof(Definition<>).MakeGenericType(new Type[] { _Argument }).GetField(_field).GetValue(null) as string) + "]")), "]", ", ", _type.Assembly.GetName().Name }); 17 | } 18 | return _type.FullName + ", " + _type.Assembly.GetName().Name; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Runtime/Dictionary/Runtime.Dictionary.Inherited.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Reflection; 4 | 5 | namespace Puresharp 6 | { 7 | static internal partial class Runtime 8 | { 9 | static internal partial class Dictionary 10 | { 11 | static public class Inherited 12 | { 13 | static public readonly Data.Map> Fields = new Data.Map>(_Type => new Data.Collection(Runtime.Lookup.Fields(_Type).OrderBy(_Field => _Field.DeclaringType.MetadataToken).ThenBy(_Field => _Field.MetadataToken).ToArray()), Concurrency.Interlocked); 14 | static public readonly Data.Map> Properties = new Data.Map>(_Type => new Data.Collection(Runtime.Lookup.Properties(_Type).OrderBy(_Property => _Property.DeclaringType.MetadataToken).ThenBy(_Property => _Property.MetadataToken).ToArray()), Concurrency.Interlocked); 15 | static public readonly Data.Map> Methods = new Data.Map>(_Type => new Data.Collection(Runtime.Lookup.Methods(_Type).OrderBy(_Method => _Method.DeclaringType.MetadataToken).ThenBy(_Method => _Method.MetadataToken).ToArray()), Concurrency.Interlocked); 16 | } 17 | } 18 | } 19 | 20 | static internal partial class Runtime 21 | { 22 | static internal partial class Dictionary 23 | { 24 | static public class Inherited 25 | { 26 | static public readonly Data.Collection Fields = Runtime.Dictionary.Inherited.Fields[Metadata.Type]; 27 | static public readonly Data.Collection Properties = Runtime.Dictionary.Inherited.Properties[Metadata.Type]; 28 | static public readonly Data.Collection Methods = Runtime.Dictionary.Inherited.Methods[Metadata.Type]; 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Runtime/Dictionary/Runtime.Dictionary.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Reflection; 4 | 5 | namespace Puresharp 6 | { 7 | static internal partial class Runtime 8 | { 9 | static internal partial class Dictionary 10 | { 11 | static public readonly Data.Map> Constructors = new Data.Map>(_Type => new Data.Collection(_Type.GetConstructors(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly).OrderBy(_Constructor => _Constructor.MetadataToken).ToArray()), Concurrency.Interlocked); 12 | static public readonly Data.Map> Fields = new Data.Map>(_Type => new Data.Collection(_Type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly).OrderBy(_Field => _Field.DeclaringType.MetadataToken).ThenBy(_Field => _Field.MetadataToken).ToArray()), Concurrency.Interlocked); 13 | static public readonly Data.Map> Properties = new Data.Map>(_Type => new Data.Collection(_Type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly).OrderBy(_Property => _Property.DeclaringType.MetadataToken).ThenBy(_Property => _Property.MetadataToken).ToArray()), Concurrency.Interlocked); 14 | static public readonly Data.Map> Methods = new Data.Map>(_Type => new Data.Collection(_Type.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly).OrderBy(_Method => _Method.DeclaringType.MetadataToken).ThenBy(_Method => _Method.MetadataToken).ToArray()), Concurrency.Interlocked); 15 | } 16 | } 17 | 18 | static internal partial class Runtime 19 | { 20 | static internal partial class Dictionary 21 | { 22 | static public readonly Data.Collection Constructors = Runtime.Dictionary.Constructors[Metadata.Type]; 23 | static public readonly Data.Collection Fields = Runtime.Dictionary.Fields[Metadata.Type]; 24 | static public readonly Data.Collection Properties = Runtime.Dictionary.Properties[Metadata.Type]; 25 | static public readonly Data.Collection Methods = Runtime.Dictionary.Methods[Metadata.Type]; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Runtime/Runtime.Inventory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Reflection; 4 | using System.Reflection.Emit; 5 | 6 | namespace Puresharp 7 | { 8 | static internal partial class Runtime 9 | { 10 | static public partial class Inventory 11 | { 12 | static private ModuleBuilder m_Module = AppDomain.CurrentDomain.DefineDynamicModule(); 13 | static private Data.Map m_Types = new Data.Map(_Type => typeof(Runtime<>).GetField(Metadata.Field(() => Metadata.Type).Name), Concurrency.Interlocked); 14 | static private Data.Map m_Fields = new Data.Map(_Field => Runtime.Inventory.Define(_Field), Concurrency.Interlocked); 15 | static private Data.Map m_Properties = new Data.Map(_Property => Runtime.Inventory.Define(_Property), Concurrency.Interlocked); 16 | static private Data.Map m_Methods = new Data.Map(_Method => Runtime.Inventory.Define(_Method), Concurrency.Interlocked); 17 | static private Data.Map m_Constructors = new Data.Map(_Constructor => Runtime.Inventory.Define(_Constructor), Concurrency.Interlocked); 18 | static private Data.Map m_Parameters = new Data.Map(_Parameter => Runtime.Inventory.Define(_Parameter), Concurrency.Interlocked); 19 | static private Data.Map m_Signatures = new Data.Map(_Method => Runtime.Inventory.Define(_Method.GetParameters()), Concurrency.Interlocked); 20 | 21 | static private FieldInfo Define(T value) 22 | { 23 | var _type = Runtime.Inventory.m_Module.DefineType($"<{ Guid.NewGuid().ToString("N") }>", TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.Abstract | TypeAttributes.Serializable); 24 | _type.DefineField($"<{ Metadata.Type.Name }>", Metadata.Type, FieldAttributes.Public | FieldAttributes.Static); 25 | var _field = _type.CreateType().GetFields().Single(); 26 | _field.SetValue(null, value); 27 | return _field; 28 | } 29 | 30 | static public FieldInfo Type(Type type) 31 | { 32 | return Runtime.Inventory.m_Types[type]; 33 | } 34 | 35 | static public FieldInfo Field(FieldInfo field) 36 | { 37 | return Runtime.Inventory.m_Fields[field]; 38 | } 39 | 40 | static public FieldInfo Property(PropertyInfo property) 41 | { 42 | return Runtime.Inventory.m_Properties[property]; 43 | } 44 | 45 | static public FieldInfo Method(MethodInfo method) 46 | { 47 | return Runtime.Inventory.m_Methods[method]; 48 | } 49 | 50 | static public FieldInfo Constructor(ConstructorInfo constructor) 51 | { 52 | return Runtime.Inventory.m_Constructors[constructor]; 53 | } 54 | 55 | static public FieldInfo Parameter(ParameterInfo parameter) 56 | { 57 | return Runtime.Inventory.m_Parameters[parameter]; 58 | } 59 | 60 | static public FieldInfo Signature(MethodBase method) 61 | { 62 | return Runtime.Inventory.m_Signatures[method]; 63 | } 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Runtime/Runtime.Lookup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Reflection; 5 | 6 | namespace Puresharp 7 | { 8 | static internal partial class Runtime 9 | { 10 | static internal partial class Lookup 11 | { 12 | static public IEnumerable Fields(Type type) 13 | { 14 | var _type = type; 15 | while (_type != null) 16 | { 17 | foreach (var _field in _type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly)) { yield return _field; } 18 | _type = _type.BaseType; 19 | } 20 | } 21 | 22 | static public IEnumerable Properties(Type type) 23 | { 24 | if (type.IsInterface) 25 | { 26 | foreach (var _property in type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly)) { yield return _property; } 27 | foreach (var _type in type.GetInterfaces()) { foreach (var _property in _type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly)) { yield return _property; } } 28 | } 29 | else 30 | { 31 | var _type = type; 32 | while (_type != null) 33 | { 34 | foreach (var _property in _type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly)) { yield return _property; } 35 | _type = _type.BaseType; 36 | } 37 | } 38 | } 39 | 40 | static public IEnumerable Methods(Type type) 41 | { 42 | if (type.IsInterface) 43 | { 44 | foreach (var _method in type.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly)) { yield return _method; } 45 | foreach (var _type in type.GetInterfaces()) { foreach (var _method in _type.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly)) { yield return _method; } } 46 | } 47 | else 48 | { 49 | var _dictionary = new HashSet(); 50 | var _type = type; 51 | while (_type != null) 52 | { 53 | foreach (var _method in _type.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly)) 54 | { 55 | if (_method.IsVirtual) 56 | { 57 | if (_dictionary.Add(_method.GetBaseDefinition())) { yield return _method; } 58 | continue; 59 | } 60 | yield return _method; 61 | } 62 | _type = _type.BaseType; 63 | } 64 | } 65 | } 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Singleton/Singleton.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puresharp 4 | { 5 | static public class Singleton 6 | where T : class, new() 7 | { 8 | static public readonly T Value = new T(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/Startup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | 5 | namespace Puresharp 6 | { 7 | [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] 8 | public partial class Startup : Attribute 9 | { 10 | static private object m_Handle = new object(); 11 | static private HashSet m_Handled = new HashSet(); 12 | 13 | static private void Run(MethodInfo method) 14 | { 15 | lock (Startup.m_Handle) 16 | { 17 | if (Startup.m_Handled.Add(method)) 18 | { 19 | (Delegate.CreateDelegate(Metadata.Type, method) as Action)(); 20 | } 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/System/Collections/Generic/Collection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Diagnostics; 5 | using System.Linq; 6 | 7 | namespace Puresharp 8 | { 9 | public sealed partial class Collection : IEnumerable 10 | { 11 | static public implicit operator T[] (Collection collection) 12 | { 13 | var _list = collection.m_List; 14 | var _length = _list.Count; 15 | var _array = new T[_length]; 16 | for (var _index = 0; _index < _length; _index++) { _array[_index] = _list[_index]; } 17 | return _array; 18 | } 19 | 20 | [DebuggerBrowsable(DebuggerBrowsableState.Never)] 21 | private IList m_List; 22 | 23 | public Collection() 24 | { 25 | this.m_List = new T[0]; 26 | } 27 | 28 | public Collection(IEnumerable enumerable) 29 | { 30 | this.m_List = new Enumerable(enumerable); 31 | } 32 | 33 | public Collection(IList list) 34 | { 35 | this.m_List = list; 36 | } 37 | 38 | public Collection(T[] array) 39 | { 40 | this.m_List = array; 41 | } 42 | 43 | public Collection(Collection collection) 44 | { 45 | this.m_List = collection.m_List; 46 | } 47 | 48 | public T this[int index] 49 | { 50 | get { return this.m_List[index]; } 51 | set { this.m_List[index] = value; } 52 | } 53 | 54 | public int Count 55 | { 56 | get { return this.m_List.Count; } 57 | } 58 | 59 | public int Index(T value) 60 | { 61 | return this.m_List.IndexOf(value); 62 | } 63 | 64 | public IEnumerator Enumerator() 65 | { 66 | return this.m_List.GetEnumerator(); 67 | } 68 | 69 | IEnumerator IEnumerable.GetEnumerator() 70 | { 71 | return this.m_List.GetEnumerator(); 72 | } 73 | 74 | IEnumerator IEnumerable.GetEnumerator() 75 | { 76 | return this.m_List.GetEnumerator(); 77 | } 78 | } 79 | 80 | [DebuggerDisplay("Count = {this.Count, nq}")] 81 | [DebuggerTypeProxy(typeof(Collection<>.Debugger))] 82 | public sealed partial class Collection 83 | { 84 | private sealed class Debugger 85 | { 86 | [DebuggerBrowsable(DebuggerBrowsableState.Never)] 87 | private Collection m_Collection; 88 | 89 | [DebuggerBrowsable(DebuggerBrowsableState.Never)] 90 | private T[] m_View; 91 | 92 | public Debugger(Collection collection) 93 | { 94 | this.m_Collection = collection; 95 | } 96 | 97 | [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] 98 | public T[] View 99 | { 100 | get 101 | { 102 | if (this.m_View == null) { this.m_View = this.m_Collection.ToArray(); } 103 | return this.m_View; 104 | } 105 | } 106 | } 107 | } 108 | } -------------------------------------------------------------------------------- /Puresharp/Puresharp/System/Collections/Generic/Enumerable.Enumerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace Puresharp 6 | { 7 | internal sealed partial class Enumerable 8 | { 9 | private sealed class Enumerator : IEnumerator 10 | { 11 | private Enumerable m_Enumerable; 12 | private IList m_Collection; 13 | private int m_Index; 14 | 15 | public Enumerator(Enumerable cache) 16 | { 17 | this.m_Enumerable = cache; 18 | this.m_Collection = m_Enumerable.m_List; 19 | this.m_Index = -1; 20 | } 21 | 22 | public T Current 23 | { 24 | get { return this.m_Collection[this.m_Index]; } 25 | } 26 | 27 | object IEnumerator.Current 28 | { 29 | get { return this.m_Collection[this.m_Index]; } 30 | } 31 | 32 | public bool MoveNext() 33 | { 34 | if (this.m_Index < this.m_Collection.Count - 1) 35 | { 36 | this.m_Index++; 37 | return true; 38 | } 39 | if (this.m_Enumerable.m_Activated) { return false; } 40 | if (this.m_Enumerable.m_Enumerator.MoveNext()) 41 | { 42 | this.m_Index++; 43 | this.m_Collection.Add(this.m_Enumerable.m_Enumerator.Current); 44 | return true; 45 | } 46 | this.m_Enumerable.m_Enumerator.Dispose(); 47 | this.m_Enumerable.m_Activated = true; 48 | return false; 49 | } 50 | 51 | public void Reset() 52 | { 53 | this.m_Index = -1; 54 | } 55 | 56 | public void Dispose() 57 | { 58 | } 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/System/Collections/Generic/Enumerable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace Puresharp 6 | { 7 | internal sealed partial class Enumerable : IList 8 | { 9 | static public implicit operator T[] (Enumerable enumerable) 10 | { 11 | var _list = enumerable.m_List; 12 | var _length = enumerable.Count; 13 | var _array = new T[_length]; 14 | for (var _index = 0; _index < _length; _index++) { _array[_index] = _list[_index]; } 15 | return _array; 16 | } 17 | 18 | private readonly IEnumerable m_Enumerable; 19 | private readonly IList m_List; 20 | private readonly IEnumerator m_Enumerator; 21 | private bool m_Activated; 22 | 23 | public Enumerable(IEnumerable enumerable) 24 | { 25 | this.m_Enumerable = enumerable; 26 | if (enumerable is IList) 27 | { 28 | this.m_Activated = true; 29 | this.m_List = enumerable as IList; 30 | } 31 | else 32 | { 33 | this.m_Enumerator = this.m_Enumerable.GetEnumerator(); 34 | this.m_List = new List(); 35 | } 36 | } 37 | 38 | public T this[int index] 39 | { 40 | get 41 | { 42 | if (this.m_Activated) { return this.m_List[index]; } 43 | var _enumerator = new Enumerable.Enumerator(this); 44 | var _index = 0; 45 | while (_enumerator.MoveNext()) 46 | { 47 | _index++; 48 | if (_index == index) { return _enumerator.Current; } 49 | } 50 | throw new IndexOutOfRangeException(); 51 | } 52 | set { throw new NotImplementedException(); } 53 | } 54 | 55 | public int Count 56 | { 57 | get 58 | { 59 | if (this.m_Activated) { return this.m_List.Count; } 60 | var _enumerator = new Enumerable.Enumerator(this); 61 | while (_enumerator.MoveNext()) { } 62 | return this.m_List.Count; 63 | } 64 | } 65 | 66 | public bool Contains(T item) 67 | { 68 | if (this.m_Activated) { return this.m_List.Contains(item); } 69 | var _enumerator = new Enumerable.Enumerator(this); 70 | while (_enumerator.MoveNext()) { if (object.Equals(_enumerator.Current, item)) { return true; } } 71 | return false; 72 | } 73 | 74 | public void CopyTo(T[] buffer, int index) 75 | { 76 | if (this.m_Activated) { this.m_List.CopyTo(buffer, index); } 77 | else 78 | { 79 | var _enumerator = new Enumerable.Enumerator(this); 80 | var _index = index; 81 | while (_enumerator.MoveNext()) { buffer[_index++] = _enumerator.Current; } 82 | } 83 | } 84 | 85 | public bool IsReadOnly 86 | { 87 | get { return true; } 88 | } 89 | 90 | public IEnumerator GetEnumerator() 91 | { 92 | if (this.m_Activated) 93 | { 94 | if (this.m_List != null) { return this.m_List.GetEnumerator(); } 95 | else { return this.m_Enumerable.GetEnumerator(); } 96 | } 97 | else { return new Enumerable.Enumerator(this); } 98 | } 99 | 100 | IEnumerator IEnumerable.GetEnumerator() 101 | { 102 | if (this.m_Activated) 103 | { 104 | if (this.m_List != null) { return this.m_List.GetEnumerator(); } 105 | else { return this.m_Enumerable.GetEnumerator(); } 106 | } 107 | else { return new Enumerable.Enumerator(this); } 108 | } 109 | 110 | public int IndexOf(T item) 111 | { 112 | throw new NotImplementedException(); 113 | } 114 | 115 | public void Insert(int index, T item) 116 | { 117 | throw new NotSupportedException(); 118 | } 119 | 120 | public void RemoveAt(int index) 121 | { 122 | throw new NotSupportedException(); 123 | } 124 | 125 | public void Add(T item) 126 | { 127 | throw new NotSupportedException(); 128 | } 129 | 130 | public void Clear() 131 | { 132 | throw new NotSupportedException(); 133 | } 134 | 135 | public bool Remove(T item) 136 | { 137 | throw new NotSupportedException(); 138 | } 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/System/Collections/Generic/Resource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace Puresharp 6 | { 7 | internal sealed class Resource 8 | { 9 | private object m_Handle = new object(); 10 | private Dictionary m_Dictionary = new Dictionary(); 11 | private Func m_Activate; 12 | 13 | public Resource(Func activate) 14 | { 15 | this.m_Activate = activate; 16 | } 17 | 18 | public TValue this[TKey key] 19 | { 20 | get 21 | { 22 | TValue _value; 23 | if (this.m_Dictionary.TryGetValue(key, out _value)) { return _value; } 24 | this.m_Dictionary.Add(key, _value = this.m_Activate(key)); 25 | return _value; 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/System/Linq/Expressions/Parameter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Linq.Expressions; 4 | 5 | namespace Puresharp 6 | { 7 | static internal class Parameter 8 | { 9 | static public readonly ParameterExpression Expression = System.Linq.Expressions.Expression.Parameter(typeof(T)); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/System/Reflection/Authority.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Reflection.Emit; 4 | 5 | namespace Puresharp 6 | { 7 | internal sealed class Authority 8 | { 9 | private delegate void Resolve(int token, out IntPtr type, out IntPtr method, out IntPtr field); 10 | private delegate Type GetTypeFromHandleUnsafe(IntPtr handle); 11 | private readonly MethodBase m_Method; 12 | private readonly Resolve m_TokenResolver; 13 | private readonly Func m_StringResolver; 14 | private readonly Func m_SignatureResolver; 15 | private readonly GetTypeFromHandleUnsafe m_GetTypeFromHandleUnsafe; 16 | private readonly MethodInfo m_GetMethodBase; 17 | private readonly ConstructorInfo m_RuntimeMethodHandleInternalCtor; 18 | private readonly ConstructorInfo m_RuntimeFieldHandleStubCtor; 19 | private readonly MethodInfo m_GetFieldInfo; 20 | 21 | public Authority(MethodBase method) 22 | { 23 | this.m_Method = method; 24 | if (method is DynamicMethod) 25 | { 26 | var _resolver = typeof(DynamicMethod).GetField("m_resolver", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(method); 27 | if (_resolver == null) throw new ArgumentException("The dynamic method's IL has not been finalized."); 28 | this.m_TokenResolver = Delegate.CreateDelegate(typeof(Resolve), _resolver, _resolver.GetType().GetMethod("ResolveToken", BindingFlags.Instance | BindingFlags.NonPublic)) as Resolve; 29 | this.m_StringResolver = Delegate.CreateDelegate(typeof(Func), _resolver, _resolver.GetType().GetMethod("GetStringLiteral", BindingFlags.Instance | BindingFlags.NonPublic)) as Func; 30 | this.m_SignatureResolver = Delegate.CreateDelegate(typeof(Func), _resolver, _resolver.GetType().GetMethod("ResolveSignature", BindingFlags.Instance | BindingFlags.NonPublic)) as Func; 31 | this.m_GetTypeFromHandleUnsafe = Delegate.CreateDelegate(typeof(GetTypeFromHandleUnsafe), typeof(Type).GetMethod("GetTypeFromHandleUnsafe", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { typeof(IntPtr) }, null)) as GetTypeFromHandleUnsafe; 32 | var _type = typeof(RuntimeTypeHandle).Assembly.GetType("System.RuntimeType"); 33 | var _method = typeof(RuntimeTypeHandle).Assembly.GetType("System.RuntimeMethodHandleInternal"); 34 | this.m_GetMethodBase = _type.GetMethod("GetMethodBase", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { _type, _method }, null); 35 | this.m_RuntimeMethodHandleInternalCtor = _method.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(IntPtr) }, null); 36 | var _stub = typeof(RuntimeTypeHandle).Assembly.GetType("System.RuntimeFieldInfoStub"); 37 | this.m_RuntimeFieldHandleStubCtor = _stub.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, new[] { typeof(IntPtr), typeof(object) }, null); 38 | this.m_GetFieldInfo = _type.GetMethod("GetFieldInfo", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { _type, typeof(RuntimeTypeHandle).Assembly.GetType("System.IRuntimeFieldInfo") }, null); 39 | } 40 | } 41 | 42 | public Type Type(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) 43 | { 44 | if (this.m_Method is DynamicMethod) 45 | { 46 | IntPtr _type, _method, _field; 47 | this.m_TokenResolver(metadataToken, out _type, out _method, out _field); 48 | return this.m_GetTypeFromHandleUnsafe(_type); 49 | } 50 | return this.m_Method.DeclaringType.Module.ResolveType(metadataToken, genericTypeArguments, genericMethodArguments); 51 | } 52 | 53 | public MethodBase Method(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) 54 | { 55 | if (this.m_Method is DynamicMethod) 56 | { 57 | IntPtr _type, _method, _field; 58 | this.m_TokenResolver(metadataToken, out _type, out _method, out _field); 59 | return (MethodBase)this.m_GetMethodBase.Invoke(null, new[] { _type == IntPtr.Zero ? null : this.m_GetTypeFromHandleUnsafe(_type), this.m_RuntimeMethodHandleInternalCtor.Invoke(new object[] { _method }) }); 60 | } 61 | return this.m_Method.DeclaringType.Module.ResolveMethod(metadataToken, genericTypeArguments, genericMethodArguments); 62 | } 63 | 64 | public FieldInfo Field(int identity, Type[] genericTypeArguments, Type[] genericMethodArguments) 65 | { 66 | if (this.m_Method is DynamicMethod) 67 | { 68 | IntPtr _type, _method, _field; 69 | this.m_TokenResolver(identity, out _type, out _method, out _field); 70 | return (FieldInfo)this.m_GetFieldInfo.Invoke(null, new[] { _type == IntPtr.Zero ? null : this.m_GetTypeFromHandleUnsafe(_type), this.m_RuntimeFieldHandleStubCtor.Invoke(new object[] { _field, null }) }); 71 | } 72 | return m_Method.DeclaringType.Module.ResolveField(identity, genericTypeArguments, genericMethodArguments); 73 | } 74 | 75 | public MemberInfo Member(int identity, Type[] genericTypeArguments, Type[] genericMethodArguments) 76 | { 77 | if (this.m_Method is DynamicMethod) 78 | { 79 | IntPtr _type, _method, _field; 80 | this.m_TokenResolver(identity, out _type, out _method, out _field); 81 | if (_method != IntPtr.Zero) { return (MethodBase)this.m_GetMethodBase.Invoke(null, new[] { _type == IntPtr.Zero ? null : this.m_GetTypeFromHandleUnsafe(_type), this.m_RuntimeMethodHandleInternalCtor.Invoke(new object[] { _method })}); } 82 | if (_field != IntPtr.Zero) { return (FieldInfo)this.m_GetFieldInfo.Invoke(null, new[] { _type == IntPtr.Zero ? null : this.m_GetTypeFromHandleUnsafe(_type), this.m_RuntimeFieldHandleStubCtor.Invoke(new object[] { _field, null }) }); } 83 | if (_type != IntPtr.Zero) { return this.m_GetTypeFromHandleUnsafe(_type); } 84 | throw new NotImplementedException("DynamicMethods are not able to reference members by token other than types, methods and fields."); 85 | } 86 | return this.m_Method.DeclaringType.Module.ResolveMember(identity, genericTypeArguments, genericMethodArguments); 87 | } 88 | 89 | public byte[] Signature(int identity) 90 | { 91 | if (this.m_Method is DynamicMethod) { return this.m_SignatureResolver(identity, 0); } 92 | return m_Method.DeclaringType.Module.ResolveSignature(identity); 93 | } 94 | 95 | public string String(int identity) 96 | { 97 | if (this.m_Method is DynamicMethod) { return this.m_StringResolver(identity); } 98 | return m_Method.DeclaringType.Module.ResolveString(identity); 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /Puresharp/Puresharp/System/Reflection/Emit/__BinaryReader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Linq; 4 | using System.Linq.Expressions; 5 | using System.Reflection; 6 | using System.Reflection.Emit; 7 | 8 | namespace Puresharp 9 | { 10 | static internal class __BinaryReader 11 | { 12 | static private readonly Func m_ConvertInt32ToLabel = Expression.Lambda>(Expression.New(Metadata