{"record":{"id":"875a621260361faa","repo":"pardeike/Harmony","slug":"could-not-create-replacement-method","errorCode":null,"errorMessage":"Could not create replacement method","messagePattern":"Could not create replacement method","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Harmony/Internal/MethodCreator.cs","lineNumber":26,"sourceCode":"\r\nnamespace HarmonyLib\r\n{\r\n\tinternal class MethodCreator\r\n\t{\r\n\t\tinternal MethodCreatorConfig config;\r\n\r\n\t\tinternal MethodCreator(MethodCreatorConfig config)\r\n\t\t{\r\n\t\t\tif (config.original is null)\r\n\t\t\t\tthrow new ArgumentNullException(\"config.original\");\r\n\t\t\tthis.config = config;\r\n\t\t\tif (config.debug)\r\n\t\t\t{\r\n\t\t\t\tFileLog.LogBuffered($\"### Patch: {config.original.FullDescription()}\");\r\n\t\t\t\tFileLog.FlushBuffer();\r\n\t\t\t}\r\n\t\t\tif (config.Prepare() == false)\r\n\t\t\t\tthrow new Exception(\"Could not create replacement method\");\r\n\t\t}\r\n\r\n\t\tinternal (MethodInfo, Dictionary<int, CodeInstruction>) CreateReplacement()\r\n\t\t{\r\n\t\t\tconfig.originalVariables = this.DeclareOriginalLocalVariables(config.MethodBase);\r\n\t\t\tconfig.localVariables = new VariableState();\r\n\r\n\t\t\tif (config.Fixes.Any() && config.returnType != typeof(void))\r\n\t\t\t{\r\n\t\t\t\tconfig.resultVariable = config.DeclareLocal(config.returnType);\r\n\t\t\t\tconfig.AddLocal(InjectionType.Result, config.resultVariable);\r\n\t\t\t\tconfig.AddCodes(this.GenerateVariableInit(config.resultVariable, true));\r\n\t\t\t}\r\n\r\n\t\t\tif (config.AnyFixHas(InjectionType.ResultRef))\r\n\t\t\t{\r\n\t\t\t\tif (config.returnType.IsByRef)\r\n\t\t\t\t{\r","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/pardeike/Harmony/blob/e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c/Harmony/Internal/MethodCreator.cs#L8-L44","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nstatic void Postfix(object __result) { } // wrong type vs int original\n// after\nstatic void Postfix(int __result) { }","handlingStrategy":"validation","validationCode":"foreach (var m in patchClass.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))\n    HarmonyMethodExtensions.CheckPatchMethod(m); // or verify signatures before registering","typeGuard":null,"tryCatchPattern":"try { harmony.PatchAll(assembly); }\ncatch (Exception ex) when (ex.Message == \"Could not create replacement method\")\n{ FileLog.Reset(); Log.Error(\"Patch signature rejected — enable Harmony debug logging and inspect patch method signatures\"); }","preventionTips":["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"],"tags":["harmony","patching","signature"],"backgroundTag":"invalid-argument-value","analyzedSha":"e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c","analyzedAt":"2026-09-15T22:47:11.550Z","contentChangedAt":"2026-09-15T22:47:11.550Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}