pardeike/Harmony · error · ArgumentException
No field found for and
Error message
No field found for {type} and {name} What it means
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.
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
Example fix
// before new CodeInstruction(OpCodes.Ldarg_0), CodeInstruction.LoadField(typeof(Player), "healt") // after new CodeInstruction(OpCodes.Ldarg_0), CodeInstruction.LoadField(typeof(Player), nameof(Player.health))
Defensive patterns
Strategy: validation
Validate before calling
if (AccessTools.Field(type, name) is null) throw new InvalidOperationException($"Field {type}.{name} not found"); Type guard
bool FieldExists(Type t, string n) => AccessTools.Field(t, n) is not null;
Try / catch
try { il.Append(CodeInstruction.LoadField(type, name)); } catch (ArgumentException ex) { log.Error($"LoadField failed for {type}.{name}", ex); throw; } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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'.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- No method found for type=
- No method found for , parameters= , generics=
- Ambiguous match for HarmonyMethod
- VariableState: variable of type
- VariableState: variable named
AI-assisted analysis of pardeike/Harmony@e7872dc170 (2026-09-15).
Data as JSON: /api/errors/553d8d14e0013622.
Report an issue: GitHub.
Appendix: source
Thrown at Harmony/Public/CodeInstruction.cs:210
il.Emit(OpCodes.Ldarg, i);
il.Emit(OpCodes.Callvirt, AccessTools.Method(typeof(T), nameof(Action.Invoke)));
il.Emit(OpCodes.Ret);
return new CodeInstruction(OpCodes.Call, closureMethod.Generate());
}
// --- FIELDS
/// <summary>Creates a CodeInstruction loading a field (LD[S]FLD[A])</summary>
/// <param name="type">The class/type where the field is defined</param>
/// <param name="name">The name of the field (case sensitive)</param>
/// <param name="useAddress">Use address of field</param>
/// <returns>A new Codeinstruction</returns>
public static CodeInstruction LoadField(Type type, string name, bool useAddress = false)
{
var field = AccessTools.Field(type, name);
if (field is null) throw new ArgumentException($"No field found for {type} and {name}");
return new CodeInstruction(useAddress ? (field.IsStatic ? OpCodes.Ldsflda : OpCodes.Ldflda) : (field.IsStatic ? OpCodes.Ldsfld : OpCodes.Ldfld), field);
}
/// <summary>Creates a CodeInstruction storing to a field (ST[S]FLD)</summary>
/// <param name="type">The class/type where the field is defined</param>
/// <param name="name">The name of the field (case sensitive)</param>
/// <returns>A new Codeinstruction</returns>
public static CodeInstruction StoreField(Type type, string name)
{
var field = AccessTools.Field(type, name);
if (field is null) throw new ArgumentException($"No field found for {type} and {name}");
return new CodeInstruction(field.IsStatic ? OpCodes.Stsfld : OpCodes.Stfld, field);
}
// --- LOCALS
/// <summary>Creates a CodeInstruction loading a local with the given index, using the shorter forms when possible</summary>
/// <param name="index">The index where the local is stored</param>
View on GitHub (pinned to e7872dc170)