Interview frequency: …
Short answer
It provides an isolation scope for loading separate .dll files and can unload them from memory (Collectibility) without stopping the main process.
Explanation
By default in .NET, an Assembly loaded in the Default Context cannot be unloaded until the whole process exits. With a subclass of AssemblyLoadContext created with isCollectible: true, you can load and run external Plugins, then call alc.Unload() and remove all references so the GC can reclaim the Assembly from memory.
Code example
var alc = new AssemblyLoadContext("PluginContext", isCollectible: true);
var assembly = alc.LoadFromAssemblyPath(pluginPath);
// Execute the code, then dispose
alc.Unload();Common mistakes
- Keeping indirect references to Plugin objects or types in the main Host, preventing its Context from unloading.