@@ -267,7 +267,7 @@ private Assembly Resolve(AssemblyLoadContext loadContext, AssemblyName assemblyN
267
267
{
268
268
#if ! UNIX
269
269
//Try loading from GAC
270
- if ( ! TryFindInGAC ( assemblyName , out asmFilePath ) )
270
+ if ( ! TryFindInGAC ( assemblyName , out asmFilePath ) )
271
271
{
272
272
return null ;
273
273
}
@@ -295,53 +295,55 @@ private Assembly Resolve(AssemblyLoadContext loadContext, AssemblyName assemblyN
295
295
// If there are multiple version of the assembly, load the latest.
296
296
private bool TryFindInGAC ( AssemblyName assemblyName , out string assemblyFilePath )
297
297
{
298
- bool assemblyFound = false ;
299
298
assemblyFilePath = null ;
299
+ if ( Internal . InternalTestHooks . DisableGACLoading )
300
+ {
301
+ return false ;
302
+ }
303
+
304
+ bool assemblyFound = false ;
300
305
char dirSeparator = IO . Path . DirectorySeparatorChar ;
301
306
302
- if ( PowerShellAssemblyLoadContextTestHooks . AllowGACLoading )
307
+ if ( String . IsNullOrEmpty ( _winDir ) )
303
308
{
304
- if ( String . IsNullOrEmpty ( _winDir ) )
305
- {
306
- //cache value of '_winDir' folder in member variable.
307
- _winDir = Environment . GetEnvironmentVariable ( "winDir" ) ;
308
- }
309
+ //cache value of '_winDir' folder in member variable.
310
+ _winDir = Environment . GetEnvironmentVariable ( "winDir" ) ;
311
+ }
309
312
310
- if ( String . IsNullOrEmpty ( _gacPathMSIL ) )
311
- {
312
- //cache value of '_gacPathMSIL' folder in member variable.
313
- _gacPathMSIL = $ "{ _winDir } { dirSeparator } Microsoft.NET{ dirSeparator } assembly{ dirSeparator } GAC_MSIL";
314
- }
313
+ if ( String . IsNullOrEmpty ( _gacPathMSIL ) )
314
+ {
315
+ //cache value of '_gacPathMSIL' folder in member variable.
316
+ _gacPathMSIL = $ "{ _winDir } { dirSeparator } Microsoft.NET{ dirSeparator } assembly{ dirSeparator } GAC_MSIL";
317
+ }
315
318
316
- assemblyFound = FindInGac ( _gacPathMSIL , assemblyName , out assemblyFilePath ) ;
319
+ assemblyFound = FindInGac ( _gacPathMSIL , assemblyName , out assemblyFilePath ) ;
317
320
318
- if ( ! assemblyFound )
319
- {
320
- string gacBitnessAwarePath = null ;
321
+ if ( ! assemblyFound )
322
+ {
323
+ string gacBitnessAwarePath = null ;
321
324
322
- if ( Environment . Is64BitProcess )
325
+ if ( Environment . Is64BitProcess )
326
+ {
327
+ if ( String . IsNullOrEmpty ( _gacPath64 ) )
323
328
{
324
- if ( String . IsNullOrEmpty ( _gacPath64 ) )
325
- {
326
- //cache value of '_gacPath64' folder in member variable.
327
- _gacPath64 = $ "{ _winDir } { dirSeparator } Microsoft.NET{ dirSeparator } assembly{ dirSeparator } GAC_64";
328
- }
329
-
330
- gacBitnessAwarePath = _gacPath64 ;
329
+ //cache value of '_gacPath64' folder in member variable.
330
+ _gacPath64 = $ "{ _winDir } { dirSeparator } Microsoft.NET{ dirSeparator } assembly{ dirSeparator } GAC_64";
331
331
}
332
- else
333
- {
334
- if ( String . IsNullOrEmpty ( _gacPath32 ) )
335
- {
336
- //cache value of '_gacPath32' folder in member variable.
337
- _gacPath32 = $ "{ _winDir } { dirSeparator } Microsoft.NET{ dirSeparator } assembly{ dirSeparator } GAC_32";
338
- }
339
332
340
- gacBitnessAwarePath = _gacPath32 ;
333
+ gacBitnessAwarePath = _gacPath64 ;
334
+ }
335
+ else
336
+ {
337
+ if ( String . IsNullOrEmpty ( _gacPath32 ) )
338
+ {
339
+ //cache value of '_gacPath32' folder in member variable.
340
+ _gacPath32 = $ "{ _winDir } { dirSeparator } Microsoft.NET{ dirSeparator } assembly{ dirSeparator } GAC_32";
341
341
}
342
342
343
- assemblyFound = FindInGac ( gacBitnessAwarePath , assemblyName , out assemblyFilePath ) ;
343
+ gacBitnessAwarePath = _gacPath32 ;
344
344
}
345
+
346
+ assemblyFound = FindInGac ( gacBitnessAwarePath , assemblyName , out assemblyFilePath ) ;
345
347
}
346
348
347
349
return assemblyFound ;
@@ -356,17 +358,17 @@ private bool FindInGac(string gacRoot, AssemblyName assemblyName, out string ass
356
358
char dirSeparator = IO . Path . DirectorySeparatorChar ;
357
359
string tempAssemblyDirPath = $ "{ gacRoot } { dirSeparator } { assemblyName . Name } ";
358
360
359
- if ( Directory . Exists ( tempAssemblyDirPath ) )
361
+ if ( Directory . Exists ( tempAssemblyDirPath ) )
360
362
{
361
363
//Enumerate all directories, sort by name and select the last. This selects the latest version.
362
364
var chosenVersionDirectory = Directory . GetDirectories ( tempAssemblyDirPath ) . OrderBy ( d => d ) . LastOrDefault ( ) ;
363
365
364
- if ( ! String . IsNullOrEmpty ( chosenVersionDirectory ) )
366
+ if ( ! String . IsNullOrEmpty ( chosenVersionDirectory ) )
365
367
{
366
368
//Select first or default as the directory will contain only one assembly. If nothing then default is null;
367
369
var foundAssemblyPath = Directory . GetFiles ( chosenVersionDirectory , $ "{ assemblyName . Name } *") . FirstOrDefault ( ) ;
368
370
369
- if ( ! String . IsNullOrEmpty ( foundAssemblyPath ) )
371
+ if ( ! String . IsNullOrEmpty ( foundAssemblyPath ) )
370
372
{
371
373
AssemblyName asmNameFound = AssemblyLoadContext . GetAssemblyName ( foundAssemblyPath ) ;
372
374
if ( IsAssemblyMatching ( assemblyName , asmNameFound ) )
@@ -515,24 +517,6 @@ public static void SetPowerShellAssemblyLoadContext([MarshalAs(UnmanagedType.LPW
515
517
PowerShellAssemblyLoadContext . InitializeSingleton ( basePaths ) ;
516
518
}
517
519
}
518
-
519
- /// <summary>
520
- /// Test hooks for PowershellAssemblyLoadContext
521
- /// </summary>
522
- public static class PowerShellAssemblyLoadContextTestHooks
523
- {
524
- internal static bool AllowGACLoading = true ;
525
-
526
- /// <summary>This member is used for internal test purposes.</summary>
527
- public static void SetTestHook ( string property , bool value )
528
- {
529
- var fieldInfo = typeof ( PowerShellAssemblyLoadContextTestHooks ) . GetField ( property , BindingFlags . Static | BindingFlags . NonPublic ) ;
530
- if ( fieldInfo != null )
531
- {
532
- fieldInfo . SetValue ( null , value ) ;
533
- }
534
- }
535
- }
536
520
}
537
521
538
522
#endif
0 commit comments