{"record":{"id":"771b2697216fadfe","repo":"pardeike/Harmony","slug":"no-such-field-defined-in-class-originaltype","errorCode":null,"errorMessage":"No such field defined in class {originalType?.AssemblyQualifiedName ?? \"null\"}","messagePattern":"No such field defined in class (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Harmony/Internal/MethodCreatorTools.cs","lineNumber":297,"sourceCode":"\t\t\t\t\t\tcodes.Add(Ldnull);\r\n\t\t\t\t\tcontinue;\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (injection.argumentMode != ArgumentMode.Original && paramRealName.StartsWith(INSTANCE_FIELD_PREFIX, StringComparison.Ordinal))\r\n\t\t\t\t{\r\n\t\t\t\t\tvar fieldName = paramRealName.Substring(INSTANCE_FIELD_PREFIX.Length);\r\n\t\t\t\t\tFieldInfo fieldInfo;\r\n\t\t\t\t\tif (fieldName.All(char.IsDigit))\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tfieldInfo = AccessTools.DeclaredField(originalType, int.Parse(fieldName));\r\n\t\t\t\t\t\tif (fieldInfo is null)\r\n\t\t\t\t\t\t\tthrow new ArgumentException($\"No field found at given index in class {originalType?.AssemblyQualifiedName ?? \"null\"}\", fieldName);\r\n\t\t\t\t\t}\r\n\t\t\t\t\telse\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tfieldInfo = AccessTools.Field(originalType, fieldName);\r\n\t\t\t\t\t\tif (fieldInfo is null)\r\n\t\t\t\t\t\t\tthrow new ArgumentException($\"No such field defined in class {originalType?.AssemblyQualifiedName ?? \"null\"}\", fieldName);\r\n\t\t\t\t\t}\r\n\r\n\t\t\t\t\tif (fieldInfo.IsStatic)\r\n\t\t\t\t\t\tcodes.Add(paramType.IsByRef ? Ldsflda[fieldInfo] : Ldsfld[fieldInfo]);\r\n\t\t\t\t\telse\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tcodes.Add(Ldarg_0);\r\n\t\t\t\t\t\tcodes.Add(paramType.IsByRef ? Ldflda[fieldInfo] : Ldfld[fieldInfo]);\r\n\t\t\t\t\t}\r\n\t\t\t\t\tcontinue;\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (injectionType == InjectionType.State)\r\n\t\t\t\t{\r\n\t\t\t\t\tvar ldlocCode = paramType.IsByRef ? OpCodes.Ldloca : OpCodes.Ldloc;\r\n\t\t\t\t\tif (config.localVariables.TryGetValue(patch.DeclaringType?.AssemblyQualifiedName ?? \"null\", out var stateVar))\r\n\t\t\t\t\t\tcodes.Add(new CodeInstruction(ldlocCode, stateVar));\r\n\t\t\t\t\telse\r","sourceCodeStart":279,"sourceCodeEnd":315,"githubUrl":"https://github.com/pardeike/Harmony/blob/e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c/Harmony/Internal/MethodCreatorTools.cs#L279-L315","documentation":"When a patch parameter name references an instance field by name (the declared-field prefix convention), Harmony looks it up with AccessTools.Field on the patched type. If no field with that name is declared on the type, this ArgumentException is thrown. It means the patch's parameter name does not correspond to any field of the target class.","triggerScenarios":"Declaring a patch parameter with the field prefix (e.g. `___someField`) where `someField` is not a field (declared) on the original type - typo, renamed field, field moved to a base class, or patching the wrong type.","commonSituations":"Game/app update renamed or moved the field; field is defined in a base type while the convention expects it declared on the patched type; case-sensitivity typos (`___HP` vs `___hp`).","solutions":["Fix the parameter name to exactly match a declared field of the target type (check AccessTools.Field(originalType, name)).","If the field lives in a base class, patch or reference accordingly / use the accessor that resolves inherited fields.","Add a pre-patch validation step that asserts the field exists before applying patches."],"exampleFix":"// before\nstatic void Postfix(Player __instance, int ___healthPoints) { ... }\n\n// after (actual field name is 'hp')\nstatic void Postfix(Player __instance, int ___hp) { ... }","handlingStrategy":"validation","validationCode":"if (AccessTools.Field(targetType, fieldName) is null)\n    throw new InvalidOperationException($\"Field '{fieldName}' missing on {targetType}\");","typeGuard":"bool HasField(Type t, string name) => AccessTools.Field(t, name) is not null;","tryCatchPattern":"try { harmony.PatchAll(); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"No such field defined in class\")) { log.Error(ex); }","preventionTips":["Copy field names directly from decompiled target code","Add a preflight scan of all ___field parameters at startup","Handle target renames behind an accessor helper"],"tags":["harmony","field-lookup","argument-binding"],"backgroundTag":"entity-not-found","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"}