UnsafeCLR 0.0.4-alpha

This is a prerelease version of UnsafeCLR.
dotnet add package UnsafeCLR --version 0.0.4-alpha
                    
NuGet\Install-Package UnsafeCLR -Version 0.0.4-alpha
                    
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="UnsafeCLR" Version="0.0.4-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="UnsafeCLR" Version="0.0.4-alpha" />
                    
Directory.Packages.props
<PackageReference Include="UnsafeCLR" />
                    
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 UnsafeCLR --version 0.0.4-alpha
                    
#r "nuget: UnsafeCLR, 0.0.4-alpha"
                    
#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 UnsafeCLR@0.0.4-alpha
                    
#: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=UnsafeCLR&version=0.0.4-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=UnsafeCLR&version=0.0.4-alpha&prerelease
                    
Install as a Cake Tool

UnsafeCLR

UnsafeCLR is a .NET library that allows for runtime method replacement (also known as method hooking or patching) on the Common Language Runtime (CLR). This can be used for advanced scenarios such as mocking, hot-patching, or instrumentation—without recompilation or modifying original source code.

⚠️ This library uses unsafe and low-level operations. Use it with caution and only when absolutely necessary.

Features

  • Replace instance methods at runtime
  • Replace static methods at runtime
  • Minimal and straightforward API
  • Compatible with modern .NET runtimes (tested on .NET 6+)

Limitations

  • Inlined methods will not be replaced, or the replacement could be permanant since the code of the called method will be baked into the caller. (A workaround is to disable JIT inlining: COMPlus_JitNoInline=1)
  • .NET 6 on ARM64 is not supported.

Getting Started

Installation

dotnet add package UnsafeCLR

API

UnsafeCLR.ReplaceInstanceMethod

public static MethodReplacement ReplaceInstanceMethod(
    Type originalInstanceType,
    MethodInfo originalMethod,
    MethodInfo replacingMethod
)
  • Replaces an instance method at runtime.

UnsafeCLR.ReplaceStaticMethod

public static MethodReplacement ReplaceStaticMethod(
    MethodInfo originalMethod,
    MethodInfo replacingMethod
)
  • Replaces a static method at runtime.

Warnings

  • This library uses low-level patching and may break with future CLR versions.
  • Not suitable for use in high-security or production-critical environments.
  • Ensure method signatures match exactly (return types and parameters).

Example Use Cases

  • Mocking static methods
  • Hot-fixing logic in running apps (debugging/instrumentation)

Example: Replace an Instance Method

using UnsafeCLR;
using System.Reflection;

class Program {
    static MethodInfo GetMethodInfo<T, TRes>(Func<T, TRes> fun) => fun.Method;
    static MethodInfo GetMethodInfo<T1, T2, TRes>(Func<T1, T2, TRes> fun) => fun.Method;
    
    public class MyClass {
        public int MyFunc(int param1) {
            // original code
            return param1 + param1;
        }
    }

    public static int MyReplacement(MyClass instance, int param1) {
        // replacing code;
        return param1 * param1;
    }

    public static void Main() {
        var instance = new MyClass();
        using (var replacement = CLRHelper.ReplaceInstanceMethod(typeof(MyClass), GetMethodInfo<int, int>(instance.MyFunc), GetMethodInfo<MyClass, int, int>(MyReplacement))) {
            Console.WriteLine("Inside 'using'");
            Console.WriteLine($"instance.MyFunc = {instance.MyFunc(10)}");
            Console.WriteLine($"MyReplacement = {MyReplacement(instance, 10)}");
            Console.WriteLine($"OriginalMethod.Invoke = { (int) replacement.OriginalMethod.Invoke(null, new object[] { instance, 10 }) }");
        }
        Console.WriteLine("Outside 'using'");
        Console.WriteLine($"instance.MyFunc = {instance.MyFunc(10)}");
        Console.WriteLine($"MyReplacement = {MyReplacement(instance, 10)}");
    }
}

License

See LICENSE.md

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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 is compatible.  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 is compatible.  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 (1)

Showing the top 1 NuGet packages that depend on UnsafeCLR:

Package Downloads
IsolatedTests

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.4-alpha 207 4/21/2025
0.0.3-alpha 168 4/19/2025
0.0.2-alpha 181 4/13/2025
0.0.1-alpha 265 4/13/2025