{"record":{"id":"8515eb03f42bbe39","repo":"JamesNK/Newtonsoft.Json","slug":"could-not-create-getter-for-0-byref-return-valu","errorCode":null,"errorMessage":"Could not create getter for {0}. ByRef return values are not supported.","messagePattern":"Could not create getter for (.+?)\\. ByRef return values are not supported\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs","lineNumber":79,"sourceCode":"            catch (Exception ex)\n            {\n                throw new JsonSerializationException(\"Error setting value to '{0}' on '{1}'.\".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), ex);\n            }\n        }\n\n        /// <summary>\n        /// Gets the value.\n        /// </summary>\n        /// <param name=\"target\">The target to get the value from.</param>\n        /// <returns>The value.</returns>\n        public object? GetValue(object target)\n        {\n            try\n            {\n                // https://github.com/dotnet/corefx/issues/26053\n                if (_memberInfo is PropertyInfo propertyInfo && propertyInfo.PropertyType.IsByRef)\n                {\n                    throw new InvalidOperationException(\"Could not create getter for {0}. ByRef return values are not supported.\".FormatWith(CultureInfo.InvariantCulture, propertyInfo));\n                }\n\n                return ReflectionUtils.GetMemberValue(_memberInfo, target);\n            }\n            catch (Exception ex)\n            {\n                throw new JsonSerializationException(\"Error getting value from '{0}' on '{1}'.\".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), ex);\n            }\n        }\n    }\n}","sourceCodeStart":61,"sourceCodeEnd":90,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs#L61-L90","documentation":"Thrown by ReflectionValueProvider.GetValue when the member is a PropertyInfo whose PropertyType.IsByRef is true (a property that returns a ref/readonly-ref value). The .NET runtime (see dotnet/corefx#26053 referenced in code) does not allow reading such a property through normal GetValue reflection, so Json.NET cannot build a getter for it.","triggerScenarios":"Serializing a type that exposes a `ref T` / `readonly ref T` property, or a property whose declared type is a by-ref-like type (rare; usually compiler-generated or interop types).","commonSituations":"Interoping with Span<T>/ref-struct-backed types that surface a by-ref property, compiler-generated readonly-ref properties in performance-oriented structs, or accidental inclusion of an internal interop property in serialization.","solutions":["Exclude the by-ref property from serialization with [JsonIgnore].","Expose the underlying value through a normal (non-ref) property and serialize that instead.","Use [JsonProperty] to redirect serialization to a different, non-by-ref member.","If the property is compiler-generated and unexpected, mark the entire type [JsonObject] with explicit member serialization to opt out of fields/opt-in."],"exampleFix":"// before: by-ref property breaks serialization\npublic unsafe struct Buffer {\n    public ref byte Data; // IsByRef == true -> 234\n}\nJsonConvert.Serialize(buffer);\n// after: project to a normal property\npublic class BufferDto {\n    [JsonIgnore] public Buffer Inner;\n    public byte Data => Inner.Data;\n}","handlingStrategy":"validation","validationCode":"foreach (var p in type.GetProperties()) if (p.PropertyType.IsByRef) throw new InvalidOperationException(p.Name + \" is ByRef; mark [JsonIgnore]\");","typeGuard":"static bool IsByRefProperty(PropertyInfo p) => p.PropertyType.IsByRef;","tryCatchPattern":"try { JsonConvert.SerializeObject(obj); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"ByRef return values are not supported\")) {\n    logger.Error(ex, \"by-ref property cannot be serialized; add [JsonIgnore].\"); throw;\n}","preventionTips":["Decorate by-ref properties with [JsonIgnore].","Project by-ref values onto normal properties for serialization DTOs.","Use explicit [JsonObject(MemberSerialization.OptIn)] to control which members serialize.","Avoid exposing ref-struct/Span-backed properties on serializable types."],"tags":["serialization","reflection","byref","interop","reflection-value-provider"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}