11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
4+ using System ;
5+ using System . IO ;
6+ using System . Reflection . PortableExecutable ;
47using ICSharpCode . Decompiler ;
58using ICSharpCode . Decompiler . CSharp ;
9+ using ICSharpCode . Decompiler . Metadata ;
610
711namespace ApiChief . Format ;
812
@@ -29,10 +33,13 @@ internal static class DecompilerFactory
2933 OptionalArguments = true ,
3034 OutVariables = true ,
3135 LiftNullables = true ,
36+ LoadInMemory = true ,
37+ ThrowOnAssemblyResolveErrors = true ,
3238 CSharpFormattingOptions = Formatter . BaselineFormatting
3339 } ;
3440
35- public static CSharpDecompiler Create ( string path ) => new ( path , _decompilerSettings ) ;
41+ public static CSharpDecompiler Create ( string path )
42+ => new ( path , CreateAssemblyResolver ( path , _decompilerSettings ) , _decompilerSettings ) ;
3643
3744 public static CSharpDecompiler CreateWithXmlComments ( string path )
3845 {
@@ -41,6 +48,29 @@ public static CSharpDecompiler CreateWithXmlComments(string path)
4148 xmlCommentsSettings . CSharpFormattingOptions = Formatter . FormattingWithXmlComments ;
4249 xmlCommentsSettings . ShowXmlDocumentation = true ;
4350
44- return new ( path , xmlCommentsSettings ) ;
51+ return new ( path , CreateAssemblyResolver ( path , xmlCommentsSettings ) , xmlCommentsSettings ) ;
52+ }
53+
54+ private static UniversalAssemblyResolver CreateAssemblyResolver ( string assemblyPath , DecompilerSettings settings )
55+ {
56+ var file = new PEFile (
57+ assemblyPath ,
58+ new FileStream ( assemblyPath , FileMode . Open , FileAccess . Read ) ,
59+ streamOptions : PEStreamOptions . PrefetchEntireImage ) ;
60+
61+ var resolver = new UniversalAssemblyResolver (
62+ assemblyPath ,
63+ settings . ThrowOnAssemblyResolveErrors ,
64+ file . DetectTargetFrameworkId ( ) ,
65+ file . DetectRuntimePack ( ) ,
66+ PEStreamOptions . PrefetchMetadata ) ;
67+
68+ var nugetPackages = Environment . GetEnvironmentVariable ( "NUGET_PACKAGES" ) ;
69+ if ( ! string . IsNullOrEmpty ( nugetPackages ) && Directory . Exists ( nugetPackages ) )
70+ {
71+ resolver . AddSearchDirectory ( nugetPackages ) ;
72+ }
73+
74+ return resolver ;
4575 }
4676}
0 commit comments