Every time the script is executed it is always compiled into an assembly and after that the assembly is loaded to a particular AppDomain for further execution. Once assembly is loaded it cannot be unloaded any more. This is the limitation of CLR, which is important to remember when hosting the script engine.
In fact the script engine itself has to handle this problem as well (e.g. when executing the script from command prompt) . It does this by relying on the fact that it is possible to unload the entire AppDomain with all loaded assembles.
The script-assembly (compiled script) is never loaded into current AppDomain. Instead, the new temporary AppDomain is created and the assembly is loaded into it. After execution the temporary AppDomain is unloaded. Thus, it is possible for the script engine to clear (delete) the script assembly when not needed (which otherwise would be locked until the script process is completed). The only exclusion from this rule is when the script is executed with /c switch or CleanupShellCommand in configuration is set. In this case script-assembly is loaded into current AppDomain because there is no need to do any cleanup.
AsmHelper class, which assists with assembly loading and browsing, supports two assembly loading modes:
Local
AsmHelper instance has to be created with a constructor receiving the
already loaded assembly (e.g. to the current AppDomain) as a parameter.
It will use this assembly for further method invocations.
Remote
AsmHelper instance has to be created with a constructor receiving the
assembly file name as a parameter. It would create temporary AppDomain
and load the assembly file into it during the first call to the
assembly method. When AsmHelper instance is disposed the temporary
AppDomain is unloaded.
While the functionality of the AsmHelper class in the local mode
is very simple the remote mode is more interesting as it can be quite
useful for working with dynamic assemblies even outside of the script
related tasks.
The tutorial Text Processor shows how to use both Local and Remote script-assembly loading techniques.
Reference | Tutorial (Text Processor)