Developer knowledge network ยท moderated exchange

Komunitas Kode Tidak Dapat Diandalkan

Riset Pengembang, Rekayasa Terbalik & Komunitas Pengkodean

Knowledge indexHidup
4Categories
919Threads
2.8KPostingan
Tutorial

Dynamic Plugin Loading and Unloading in .NET using AssemblyLoadContext [StackOverflow Architecture Guide]

dotnet_runtime_architect
.NET Core Specialist
MEMBER
Reputasi: 103
Tanggal Bergabung: Apr 2018
Postingan: 40
Terima kasih: 24
1 bulan yang lalu ยท Jul 10, 2026 5:25 AM
#1

How to load and completely unload third-party C# plugins from RAM without restarting the host process:

CSHARP
public class PluginLoadContext : AssemblyLoadContext
{
    public PluginLoadContext() : base(isCollectible: true) { }
}

// Loading & Unloading
var context = new PluginLoadContext();
Assembly pluginAssembly = context.LoadFromAssemblyPath("/plugins/CustomTool.dll");
// Execute plugin logic...

context.Unload(); // Unloads assembly and frees metadata from memory!
GC.Collect();
GC.WaitForPendingFinalizers();

isCollectible: true allows the Garbage Collector to fully reclaim JIT-compiled code and type metadata.

unsafe_memory_wizard
Unsafe & P/Invoke
MEMBER
Reputasi: 160
Tanggal Bergabung: Jul 2022
Postingan: 10
Terima kasih: 38
1 bulan yang lalu ยท Jul 10, 2026 9:45 AM
#2

Crucial tip: Ensure zero references (instances, delegates, types) remain in the host process, or the ALC will stay pinned in memory.

roslyn_source_gen
Roslyn Compiler Dev
MEMBER
Reputasi: 120
Tanggal Bergabung: Feb 2020
Postingan: 12
Terima kasih: 75
1 bulan yang lalu ยท Jul 10, 2026 10:19 PM
#3

Used this to implement hot-reloading game scripts in our engine without restarting server processes.