{"record":{"id":"e5b98353fcf4beb0","repo":"pardeike/Harmony","slug":"field-must-not-be-static","errorCode":null,"errorMessage":"Field must not be static","messagePattern":"Field must not be static","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Harmony/Tools/AccessTools.cs","lineNumber":1489,"sourceCode":"\t\t/// <para>\r\n\t\t/// For backwards compatibility, there is no class constraint on <typeparamref name=\"T\"/>.\r\n\t\t/// Instead, the non-value-type check is done at runtime within the method.\r\n\t\t/// </para>\r\n\t\t/// </remarks>\r\n\t\t///\r\n\t\tpublic static ref F FieldRefAccess<T, F>(T instance, FieldInfo fieldInfo)\r\n\t\t{\r\n\t\t\tif (instance is null)\r\n\t\t\t\tthrow new ArgumentNullException(nameof(instance));\r\n\t\t\tif (fieldInfo is null)\r\n\t\t\t\tthrow new ArgumentNullException(nameof(fieldInfo));\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tvar delegateInstanceType = typeof(T);\r\n\t\t\t\tif (delegateInstanceType.IsValueType)\r\n\t\t\t\t\tthrow new ArgumentException(\"T (FieldRefAccess instance type) must not be a value type\");\r\n\t\t\t\tif (fieldInfo.IsStatic)\r\n\t\t\t\t\tthrow new ArgumentException(\"Field must not be static\");\r\n\t\t\t\tvar needCastclass = false;\r\n\t\t\t\tif (fieldInfo.DeclaringType is Type declaringType)\r\n\t\t\t\t{\r\n\t\t\t\t\t// When fieldInfo is passed to FieldRefAccess methods, the T generic class constraint is insufficient to ensure that\r\n\t\t\t\t\t// the field is not a struct instance field, since T could be object, ValueType, or an interface that the struct implements.\r\n\t\t\t\t\tif (declaringType.IsValueType)\r\n\t\t\t\t\t\tthrow new ArgumentException(\"FieldDeclaringType must be a class\");\r\n\t\t\t\t\tneedCastclass = Tools.FieldRefNeedsClasscast(delegateInstanceType, declaringType);\r\n\t\t\t\t}\r\n\t\t\t\treturn ref Tools.FieldRefAccess<T, F>(fieldInfo, needCastclass)(instance);\r\n\t\t\t}\r\n\t\t\tcatch (Exception ex)\r\n\t\t\t{\r\n\t\t\t\tthrow new ArgumentException($\"FieldRefAccess<{typeof(T)}, {typeof(F)}> for {instance}, {fieldInfo} caused an exception\", ex);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\t/// <summary>A readable/assignable reference delegate to an instance field of a struct</summary>\r","sourceCodeStart":1471,"sourceCodeEnd":1507,"githubUrl":"https://github.com/pardeike/Harmony/blob/e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c/Harmony/Tools/AccessTools.cs#L1471-L1507","documentation":"The instance FieldRefAccess overloads only support instance fields; a static field has no instance operand. Harmony throws this ArgumentException when fieldInfo.IsStatic is true to prevent silently wrong ref semantics.","triggerScenarios":"Calling AccessTools.FieldRefAccess<T,F>(fieldInfo, instance) (or FieldRefAccess<T,F>(fieldInfo) with instance semantics) with a FieldInfo whose IsStatic is true, e.g. obtained via BindingFlags.FlattenHierarchy | Static.","commonSituations":"A field changed from instance to static in a target-library update; field lookup using broad BindingFlags that return the static field.","solutions":["Use the static field accessor: AccessTools.FieldRefAccess<F>(fieldInfo) / StaticFieldRefAccess overloads","Re-check the target assembly version and the field's staticness","Fix BindingFlags in the field lookup (remove Static or add Instance explicitly)","Pre-validate with fieldInfo.IsStatic before choosing the overload"],"exampleFix":"// before\nvar rf = AccessTools.FieldRefAccess<MyClass, int>(staticFi); // static field\n// after\nvar rf = AccessTools.StaticFieldRefAccess<MyClass, int>(staticFi);","handlingStrategy":"validation","validationCode":"if (fieldInfo is { IsStatic: true }) return AccessTools.StaticFieldRefAccess<F>(fieldInfo); // branch instead of instance overload","typeGuard":"static bool IsInstanceField(FieldInfo fi) => fi is { IsStatic: false };","tryCatchPattern":null,"preventionTips":["Use precise BindingFlags (Instance for instance fields, Static for static)","Re-check field staticness when upgrading the patched library","Branch static vs instance accessors at delegate-creation time"],"tags":["harmony","reflection","static-field","fieldref"],"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"}