@@ -51,7 +51,12 @@ public class CSharpLoader
5151 public static AssemblyLoadContext AssemblyContextDefault { get ; set ; } = AssemblyLoadContext . Default ;
5252 public AssemblyLoadContext AssemblyContext { get ; set ; } = AssemblyContextDefault ;
5353
54- public static List < string > DefaultSearchPaths { get ; set ; } = new ( ) { "bin" } ;
54+ public static List < string > DefaultSearchPaths { get ; set ; } = [
55+ Environment . CurrentDirectory ,
56+ "bin" ,
57+ AppContext . BaseDirectory ,
58+ Path . Combine ( AppContext . BaseDirectory , "bin" ) ,
59+ ] ;
5560 public List < string > SearchPaths { get ; set ; } = DefaultSearchPaths ;
5661
5762 public IFrameworkResolver FrameworkResolver { get ; set ; } = new DefaultFrameworkResolver ( ) ;
@@ -128,10 +133,10 @@ IEnumerable<MetadataReference> LoadExternalRefs(string path)
128133
129134 var filename = Path . GetFileName ( ref_file ) ;
130135
131- var full_path = ResolveFile ( ref_file ) ;
136+ var full_path = TryResolveFile ( ref_file ) ;
132137 var sys_path = Path . Combine ( assemblyPath , ref_file ) ;
133138
134- if ( File . Exists ( full_path ) )
139+ if ( full_path is not null && File . Exists ( full_path ) )
135140 yield return MetadataReference . CreateFromFile ( full_path ) ;
136141
137142 else if ( File . Exists ( sys_path ) )
@@ -204,9 +209,8 @@ public IEnumerable<MetadataReference> GetAllSystemReferences()
204209 return systemRefs = files . Select ( f => MetadataReference . CreateFromFile ( Path . GetFullPath ( f ) ) ) ;
205210 }
206211
207- public string ResolveFile ( string path )
212+ public string ? TryResolveFile ( string path )
208213 {
209- var dir = Path . GetDirectoryName ( path ) ;
210214 var filename = Path . GetFileName ( path ) ;
211215
212216 foreach ( var searchPath in SearchPaths )
@@ -218,35 +222,23 @@ public string ResolveFile(string path)
218222 }
219223
220224 var spmatches = Directory . GetFiles ( searchPath , filename , SearchOption . AllDirectories ) ;
221- if ( spmatches . Any ( ) )
225+ if ( spmatches . Length != 0 )
222226 {
223227 var match = spmatches . First ( ) ;
224- if ( File . Exists ( match ) )
225- {
226- return new FileInfo ( match ) . FullName ;
227- }
228+ var fullPath = Path . GetFullPath ( match ) ;
229+ if ( File . Exists ( fullPath ) )
230+ return fullPath ;
228231 }
229232 }
230233
231- if ( String . IsNullOrWhiteSpace ( dir ) )
232- {
233- if ( ! File . Exists ( path ) )
234- path = Path . Combine ( Environment . CurrentDirectory , filename ) ;
235- else if ( ! File . Exists ( path ) )
236- path = Path . Combine ( Environment . CurrentDirectory , "bin" , filename ) ;
237- else if ( ! File . Exists ( path ) )
238- path = Path . Combine ( AppContext . BaseDirectory , filename ) ;
239- }
240- else path = Path . Combine ( dir , filename ) ;
241-
242- return new FileInfo ( path ) . FullName ;
234+ return null ;
243235 }
244236
245237 public MetadataReference ? TryCreateRefFromFile ( string path )
246238 {
247- path = ResolveFile ( path ) ;
248- if ( File . Exists ( path ) )
249- return MetadataReference . CreateFromFile ( path ) ;
239+ var resolved = TryResolveFile ( path ) ;
240+ if ( resolved is not null && File . Exists ( resolved ) )
241+ return MetadataReference . CreateFromFile ( resolved ) ;
250242 return null ;
251243 }
252244
0 commit comments