Lead.Hook 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Lead.Hook --version 1.0.0
                    
NuGet\Install-Package Lead.Hook -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Lead.Hook" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lead.Hook" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Lead.Hook" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Lead.Hook --version 1.0.0
                    
#r "nuget: Lead.Hook, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Lead.Hook@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Lead.Hook&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Lead.Hook&version=1.0.0
                    
Install as a Cake Tool

Lead.Hook

A lightweight IL-rewriting hook engine for .NET 8+ and .NET 10 — no Harmony, no runtime patching, no native hooks.

How It Works

Lead.Hook rewrites IL instructions at assembly load time using Mono.Cecil. It scans target assemblies for specific IL patterns and replaces them with calls to your replacement methods — before the code ever executes.

Supported Hook Types

HookType IL Instruction Description
CallSite call / callvirt Redirect method calls to your replacement
MethodBody Method IL body Replace entire method body (works with static classes, reflection-safe)
NewObj newobj Intercept object creation, replace with factory method
FieldRead ldfld / ldsfld Intercept field read access
FieldWrite stfld / stsfld Intercept field write access
TypeCheck isinst / castclass Intercept as/is type checks
Box box / unbox.any Intercept boxing/unboxing
FunctionPointer ldftn / ldvirtftn Intercept function pointer acquisition

Quick Start

using Lead.Hook;

var engine = new HookBuilder()
    // CallSite: redirect method call
    .Hook("MyApp.TextPrinter", "GetText")
        .With(typeof(MyReplacement), "GetText")

    // MethodBody: replace entire method body (static class safe)
    .Hook("MyApp.SecretKeeper", "GetSecret", HookType.MethodBody)
        .With(typeof(SecretReplacement), "GetSecret")

    // NewObj: intercept constructor
    .Hook("MyApp.ConfigLoader", ".ctor", HookType.NewObj)
        .With(typeof(ConfigFactory), "Create")

    // FieldRead: intercept field access
    .Hook("MyApp.UserProfile", "Name", HookType.FieldRead)
        .With(typeof(FieldHooks), "GetName")

    .Build();

var result = engine.RewriteWithResult("TargetApp.dll");
// Load result.RewrittenAssembly via AssemblyLoadContext

Replacement Method Signatures

// CallSite (instance method): first param is 'this'
public static int Add(object self, int x) => 0;

// CallSite (static method): no 'this'
public static string GetText() => "replaced";

// MethodBody: same as CallSite
public static string GetSecret() => "replaced";

// NewObj: same params as constructor, returns replacement object
public static ConfigShadow Create(string env) => new("hacked-" + env);

// FieldRead (instance): receives 'this'
public static string GetName(object self) => "replaced";

// FieldRead (static): no params
public static string GetRole() => "admin";

// FieldWrite (instance): receives 'this' + value
public static void SetName(object self, string value) { }

// FieldWrite (static): receives value
public static void SetRole(string value) { }

// TypeCheck: receives object, returns object?
public static object? CheckType(object obj) => obj;

// Box: receives value type, returns object
public static object BoxInt(int value) => value;

// Unbox: receives object, returns value type
public static int UnboxInt(object value) => (int)value;

// FunctionPointer: returns IntPtr
public static IntPtr GetPtr() => IntPtr.Zero;

.NET 10 Compatibility

Lead.Hook targets net8.0 and is fully compatible with .NET 10 applications. No known breaking issues.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 162 5/31/2026
1.2.0 152 5/30/2026
1.1.0 146 5/30/2026
1.0.0 153 5/30/2026