Developer knowledge network · moderated exchange

UnreliableCode コミュニティ

開発者リサーチ、リバース エンジニアリング、コーディング コミュニティ

Tutorial

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

dotnet_runtime_architect
.NET Core Specialist
MEMBER
担当者: 103
参加日: Apr 2018
投稿: 40
ありがとう: 24
1 か月前 · 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
担当者: 160
参加日: Jul 2022
投稿: 10
ありがとう: 38
1 か月前 · 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
担当者: 120
参加日: Feb 2020
投稿: 12
ありがとう: 75
1 か月前 · Jul 10, 2026 10:19 PM
#3

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