{"record":{"id":"553d8d14e0013622","repo":"pardeike/Harmony","slug":"no-field-found-for-type-and-name","errorCode":null,"errorMessage":"No field found for {type} and {name}","messagePattern":"No field found for (.+?) and (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Harmony/Public/CodeInstruction.cs","lineNumber":210,"sourceCode":"\t\t\t\til.Emit(OpCodes.Ldarg, i);\r\n\r\n\t\t\til.Emit(OpCodes.Callvirt, AccessTools.Method(typeof(T), nameof(Action.Invoke)));\r\n\t\t\til.Emit(OpCodes.Ret);\r\n\r\n\t\t\treturn new CodeInstruction(OpCodes.Call, closureMethod.Generate());\r\n\t\t}\r\n\r\n\t\t// --- FIELDS\r\n\r\n\t\t/// <summary>Creates a CodeInstruction loading a field (LD[S]FLD[A])</summary>\r\n\t\t/// <param name=\"type\">The class/type where the field is defined</param>\r\n\t\t/// <param name=\"name\">The name of the field (case sensitive)</param>\r\n\t\t/// <param name=\"useAddress\">Use address of field</param>\r\n\t\t/// <returns>A new Codeinstruction</returns>\r\n\t\tpublic static CodeInstruction LoadField(Type type, string name, bool useAddress = false)\r\n\t\t{\r\n\t\t\tvar field = AccessTools.Field(type, name);\r\n\t\t\tif (field is null) throw new ArgumentException($\"No field found for {type} and {name}\");\r\n\t\t\treturn new CodeInstruction(useAddress ? (field.IsStatic ? OpCodes.Ldsflda : OpCodes.Ldflda) : (field.IsStatic ? OpCodes.Ldsfld : OpCodes.Ldfld), field);\r\n\t\t}\r\n\r\n\t\t/// <summary>Creates a CodeInstruction storing to a field (ST[S]FLD)</summary>\r\n\t\t/// <param name=\"type\">The class/type where the field is defined</param>\r\n\t\t/// <param name=\"name\">The name of the field (case sensitive)</param>\r\n\t\t/// <returns>A new Codeinstruction</returns>\r\n\t\tpublic static CodeInstruction StoreField(Type type, string name)\r\n\t\t{\r\n\t\t\tvar field = AccessTools.Field(type, name);\r\n\t\t\tif (field is null) throw new ArgumentException($\"No field found for {type} and {name}\");\r\n\t\t\treturn new CodeInstruction(field.IsStatic ? OpCodes.Stsfld : OpCodes.Stfld, field);\r\n\t\t}\r\n\r\n\t\t// --- LOCALS\r\n\r\n\t\t/// <summary>Creates a CodeInstruction loading a local with the given index, using the shorter forms when possible</summary>\r\n\t\t/// <param name=\"index\">The index where the local is stored</param>\r","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/pardeike/Harmony/blob/e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c/Harmony/Public/CodeInstruction.cs#L192-L228","documentation":"CodeInstruction.LoadField resolves the field with AccessTools.Field and emits an LDFLD/LDSFLD (or address variant). If the field does not exist on the type, Harmony throws ArgumentException instead of emitting an invalid field load.","triggerScenarios":"CodeInstruction.LoadField(typeof(T), \"fieldName\") where the field was renamed, removed, is defined on a base/derived type not resolved, or the name's casing differs.","commonSituations":"Target mod/library version changed private field names; typo in the field name; trying to auto-load a property or auto-property backing field using the property name instead of '<Name>k__BackingField'.","solutions":["Correct the field name (exact, case-sensitive) and confirm it exists with AccessTools.Field(type, name)","If targeting a property, use CodeInstruction.LoadArgument/Call on its getter or the backing-field name instead","AccessTools.Field on the declaring type where the field is actually defined","Validate at patch init and fall back to a safe path or log if the field vanished in a new target version"],"exampleFix":"// before\nnew CodeInstruction(OpCodes.Ldarg_0), CodeInstruction.LoadField(typeof(Player), \"healt\")\n// after\nnew CodeInstruction(OpCodes.Ldarg_0), CodeInstruction.LoadField(typeof(Player), nameof(Player.health))","handlingStrategy":"validation","validationCode":"if (AccessTools.Field(type, name) is null) throw new InvalidOperationException($\"Field {type}.{name} not found\");","typeGuard":"bool FieldExists(Type t, string n) => AccessTools.Field(t, n) is not null;","tryCatchPattern":"try { il.Append(CodeInstruction.LoadField(type, name)); } catch (ArgumentException ex) { log.Error($\"LoadField failed for {type}.{name}\", ex); throw; }","preventionTips":["Use nameof() for compile-time-checked field names","Confirm declaring type — fields may live on base types","Null-check AccessTools.Field at patch startup across supported target versions"],"tags":["transpiler","reflection","fields","csharp"],"backgroundTag":"resource-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"}