{"record":{"id":"8e886b1e79a2ce04","repo":"pardeike/Harmony","slug":"no-field-found-at-given-index-in-class-originaltype","errorCode":null,"errorMessage":"No field found at given index in class {originalType?.AssemblyQualifiedName ?? \"null\"}","messagePattern":"No field found at given index in class (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Harmony/Internal/MethodCreatorTools.cs","lineNumber":291,"sourceCode":"\r\n\t\t\t\tif (injectionType == InjectionType.ArgsArray)\r\n\t\t\t\t{\r\n\t\t\t\t\tif (config.localVariables.TryGetValue(InjectionType.ArgsArray, out var argsArrayVar))\r\n\t\t\t\t\t\tcodes.Add(Ldloc[argsArrayVar]);\r\n\t\t\t\t\telse\r\n\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","sourceCodeStart":273,"sourceCodeEnd":309,"githubUrl":"https://github.com/pardeike/Harmony/blob/e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c/Harmony/Internal/MethodCreatorTools.cs#L273-L309","documentation":"When binding an injected patch parameter whose name uses the instance-field-by-index convention (`__N` derived from INSTANCE_FIELD_PREFIX), Harmony resolves the Nth declared field of the patched type via AccessTools.DeclaredField. If no declared field exists at that index (type has fewer fields, or ordering changed), this ArgumentException is thrown with the field's argument name.","triggerScenarios":"Naming a patch parameter with the instance-field numeric prefix (e.g. `__0`, `__2`) in a patch for a type that has no declared field at that index; the target type changed between versions so field order/count no longer matches.","commonSituations":"Target assembly updated and fields reordered/removed; patch written against a different class than intended; using the index form on a struct/class where all fields are inherited rather than declared.","solutions":["Reduce the index or verify the target type's declared field count/order at runtime (e.g. via AccessTools.GetDeclaredFields(originalType)).","Prefer referencing the field by name instead of index to be resilient to reordering.","Pin the target assembly version or add a startup check that the expected field exists before applying the patch."],"exampleFix":"// before\nstatic void Postfix(MyClass __this, int __5) { ... }\n\n// after\nstatic void Postfix(MyClass __this, int ___myFieldName) { ... }","handlingStrategy":"validation","validationCode":"var fields = AccessTools.GetDeclaredFields(targetType);\nif (index < 0 || index >= fields.Count)\n    throw new InvalidOperationException($\"{targetType} has no declared field at index {index}\");","typeGuard":"bool HasFieldAtIndex(Type t, int i) => AccessTools.DeclaredField(t, i) is not null;","tryCatchPattern":"try { harmony.PatchAll(); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"No field found at given index\")) { log.Error(ex); }","preventionTips":["Reference fields by name, not index, for version resilience","Re-verify field order after every target update","Gate patches on a target-version check"],"tags":["harmony","field-lookup","argument-binding","index-out-of-range"],"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"}