pardeike/Harmony · error · Exception
Could not create replacement method
Error message
Could not create replacement method
What it means
After the MethodCreator config runs its Prepare() step (declaring locals, resolving patch methods, computing which injections are used), a false result means the replacement method could not even be set up — for example a patch method's signature is fundamentally incompatible with the original. Harmony throws this generic Exception because the config cannot produce a valid dynamic method.
Solutions
- Check every patch method signature: only use documented parameter names (__0, __instance, __result, __state, __originalMethod, etc.) with assignable types
- Enable Harmony debugging (FileLog.Debug / Harmony.DEBUG = true) to see the detailed reason Prepare() failed
- Upgrade Harmony to the latest version, as signature-binding diagnostics have improved
- Reduce the patch to a minimal empty prefix/postfix and re-add parameters until the failure point is identified
Example fix
// before
static void Postfix(object __result) { } // wrong type vs int original
// after
static void Postfix(int __result) { } Defensive patterns
Strategy: validation
Validate before calling
foreach (var m in patchClass.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
HarmonyMethodExtensions.CheckPatchMethod(m); // or verify signatures before registering Try / catch
try { harmony.PatchAll(assembly); }
catch (Exception ex) when (ex.Message == "Could not create replacement method")
{ FileLog.Reset(); Log.Error("Patch signature rejected — enable Harmony debug logging and inspect patch method signatures"); } Prevention
- Use only documented injection parameter names (__instance, __result, __state, __0...) with matching types
- Set Harmony.DEBUG/FileLog debug on in development builds to see Prepare() details
- Upgrade Harmony when upgrading the host game/framework
- Start with a minimal prefix and add parameters incrementally
When it happens
Trigger: A prefix/postfix/finalizer method with a signature Harmony cannot bind (bad parameter types, missing original method info); config.Prepare() failing to resolve required injection parameters; patching with methods whose declaring type or generics cannot be reconciled.
Common situations: Typo'd parameter names in patch methods (e.g. __instance/__result misspellings resolve differently); patch methods with unsupported signatures after a Harmony version upgrade; generics mismatches between patch and target.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- __state type mismatch in patch
- Prefix patch has not "bool" or "void" return type
- Return type of pass through postfix
- Postfix patch must have a "void" return type
- Cannot create replacement for
AI-assisted analysis of pardeike/Harmony@e7872dc170 (2026-09-15).
Data as JSON: /api/errors/875a621260361faa.
Report an issue: GitHub.
Appendix: source
Thrown at Harmony/Internal/MethodCreator.cs:26
namespace HarmonyLib
{
internal class MethodCreator
{
internal MethodCreatorConfig config;
internal MethodCreator(MethodCreatorConfig config)
{
if (config.original is null)
throw new ArgumentNullException("config.original");
this.config = config;
if (config.debug)
{
FileLog.LogBuffered($"### Patch: {config.original.FullDescription()}");
FileLog.FlushBuffer();
}
if (config.Prepare() == false)
throw new Exception("Could not create replacement method");
}
internal (MethodInfo, Dictionary<int, CodeInstruction>) CreateReplacement()
{
config.originalVariables = this.DeclareOriginalLocalVariables(config.MethodBase);
config.localVariables = new VariableState();
if (config.Fixes.Any() && config.returnType != typeof(void))
{
config.resultVariable = config.DeclareLocal(config.returnType);
config.AddLocal(InjectionType.Result, config.resultVariable);
config.AddCodes(this.GenerateVariableInit(config.resultVariable, true));
}
if (config.AnyFixHas(InjectionType.ResultRef))
{
if (config.returnType.IsByRef)
{
View on GitHub (pinned to e7872dc170)