@@ -59,6 +59,7 @@ internal class Coverage
5959 private readonly CoverageParameters _parameters ;
6060
6161 public string Identifier { get ; }
62+ private List < string > unloadedModules { get ; set ; }
6263
6364 public Coverage ( string moduleOrDirectory ,
6465 CoverageParameters parameters ,
@@ -78,6 +79,7 @@ public Coverage(string moduleOrDirectory,
7879 _cecilSymbolHelper = cecilSymbolHelper ;
7980 Identifier = Guid . NewGuid ( ) . ToString ( ) ;
8081 _results = new List < InstrumenterResult > ( ) ;
82+ unloadedModules = new List < string > ( ) ;
8183 }
8284
8385 public Coverage ( CoveragePrepareResult prepareResult ,
@@ -241,7 +243,10 @@ public CoverageResult GetCoverageResult()
241243 }
242244
243245 modules . Add ( Path . GetFileName ( result . ModulePath ) , documents ) ;
244- _instrumentationHelper . RestoreOriginalModule ( result . ModulePath , Identifier ) ;
246+ if ( ! unloadedModules . Contains ( result . ModulePath ) )
247+ {
248+ UnloadModule ( result . ModulePath ) ;
249+ }
245250 }
246251
247252 // In case of anonymous delegate compiler generate a custom class and passes it as type.method delegate.
@@ -326,6 +331,16 @@ public CoverageResult GetCoverageResult()
326331 return coverageResult ;
327332 }
328333
334+ /// <summary>
335+ /// Manually invoke the unloading of modules and restoration of the original assembly files
336+ /// </summary>
337+ /// <param name="modulePath"></param>
338+ public void UnloadModule ( string modulePath )
339+ {
340+ unloadedModules . Add ( modulePath ) ;
341+ _instrumentationHelper . RestoreOriginalModule ( modulePath , Identifier ) ;
342+ }
343+
329344 private bool BranchInCompilerGeneratedClass ( string methodName )
330345 {
331346 foreach ( InstrumenterResult instrumentedResult in _results )
0 commit comments