{"record":{"id":"0b1e9bb2713f77a4","repo":"pardeike/Harmony","slug":"cannot-set-value-of-method-method-fulldescription","errorCode":null,"errorMessage":"cannot set value of method {_method.FullDescription()}","messagePattern":"cannot set value of method (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Harmony/Tools/Traverse.cs","lineNumber":170,"sourceCode":"\t\tpublic T GetValue<T>(params object[] arguments)\r\n\t\t{\r\n\t\t\tif (_method is null)\r\n\t\t\t\tthrow new Exception(\"cannot get method value without method\");\r\n\t\t\treturn (T)_method.Invoke(_root, arguments);\r\n\t\t}\r\n\r\n\t\t/// <summary>Sets a value of the current field or property</summary>\r\n\t\t/// <param name=\"value\">The value</param>\r\n\t\t/// <returns>The same traverse instance</returns>\r\n\t\t///\r\n\t\tpublic Traverse SetValue(object value)\r\n\t\t{\r\n\t\t\tif (_info is FieldInfo)\r\n\t\t\t\t((FieldInfo)_info).SetValue(_root, value, AccessTools.all, null, CultureInfo.CurrentCulture);\r\n\t\t\tif (_info is PropertyInfo)\r\n\t\t\t\t((PropertyInfo)_info).SetValue(_root, value, AccessTools.all, null, _params, CultureInfo.CurrentCulture);\r\n\t\t\tif (_method is not null)\r\n\t\t\t\tthrow new Exception($\"cannot set value of method {_method.FullDescription()}\");\r\n\t\t\treturn this;\r\n\t\t}\r\n\r\n\t\t/// <summary>Gets the type of the current field or property</summary>\r\n\t\t/// <returns>The type</returns>\r\n\t\t///\r\n\t\tpublic Type GetValueType()\r\n\t\t{\r\n\t\t\tif (_info is FieldInfo)\r\n\t\t\t\treturn ((FieldInfo)_info).FieldType;\r\n\t\t\tif (_info is PropertyInfo)\r\n\t\t\t\treturn ((PropertyInfo)_info).PropertyType;\r\n\t\t\treturn null;\r\n\t\t}\r\n\r\n\t\t/// <summary>Checks if the current traverse instance is for a field</summary>\r\n\t\t/// <returns>True if its a field</returns>\r\n\t\t///\r","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/pardeike/Harmony/blob/e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c/Harmony/Tools/Traverse.cs#L152-L188","documentation":"Traverse.SetValue can only write fields and properties. If the traversal's current member is a method, there is no settable value, so Harmony throws with the method's full description to identify the offending path member.","triggerScenarios":"Calling traverse.SetValue(value) (or the .Value shortcut) on a traverse positioned on a method via .Method(...), instead of on a field or property.","commonSituations":"Reusing a traverse variable that was last pointed at a method; confusing .GetValue() (method invoke) with .Value (field/property access); property with no setter resolved oddly so only the getter method was found.","solutions":["Re-position the traverse on the field/property with .Field()/.Property() before SetValue","Use a separate Traverse instance for method invocation vs value access","Check the target member is not a method: Traverse.Create(t).Field(\"f\").SetValue(x)"],"exampleFix":"// before\nvar tr = Traverse.Create(f).Method(\"Compute\"); tr.Value = 5; // method has no value\n// after\nTraverse.Create(f).Field(\"result\").SetValue(5);","handlingStrategy":"validation","validationCode":"var member = Traverse.Create(obj).Field(\"result\");\nif (!member.FieldExists()) throw new InvalidOperationException(\"target is not a settable field/property\");\nmember.SetValue(5);","typeGuard":"bool IsSettableMember(this Traverse tr) => tr.FieldExists() || tr.PropertyExists();","tryCatchPattern":"try { tr.Value = value; }\ncatch (Exception ex) when (ex.Message.StartsWith(\"cannot set value of method\")) { /* re-point traverse at a field/property */ }","preventionTips":["Use separate Traverse instances for method calls and value writes","Never call SetValue/.Value on a traverse positioned by .Method()","Verify with FieldExists/PropertyExists before writing"],"tags":["csharp","reflection","traverse"],"backgroundTag":"unsupported-operation","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"}