Developer knowledge network · moderated exchange

UnreliableCode Topluluğu

Geliştirici Araştırması, Tersine Mühendislik ve Kodlama Topluluğu

Knowledge indexCanlı
4Categories
919Threads
2.8KGönderiler
Tutorial

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

dotnet_runtime_architect
.NET Core Specialist
MEMBER
Temsilci: 103
Katılım Tarihi: Apr 2018
Gönderiler: 40
Teşekkürler: 24
1 ay önce · 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
Temsilci: 160
Katılım Tarihi: Jul 2022
Gönderiler: 10
Teşekkürler: 38
1 ay önce · 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
Temsilci: 120
Katılım Tarihi: Feb 2020
Gönderiler: 12
Teşekkürler: 75
1 ay önce · Jul 10, 2026 10:19 PM
#3

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