{"record":{"id":"2d33ec023c369254","repo":"JamesNK/Newtonsoft.Json","slug":"could-not-create-getter-for-0-byref-return-valu-2d33ec","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/Utilities/ReflectionDelegateFactory.cs","lineNumber":48,"sourceCode":"using Newtonsoft.Json.Serialization;\n\n#if !HAVE_LINQ\nusing Newtonsoft.Json.Utilities.LinqBridge;\n#endif\n\nnamespace Newtonsoft.Json.Utilities\n{\n    internal abstract class ReflectionDelegateFactory\n    {\n        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n        public Func<T, object?> CreateGet<T>(MemberInfo memberInfo)\n        {\n            if (memberInfo is PropertyInfo propertyInfo)\n            {\n                // https://github.com/dotnet/corefx/issues/26053\n                if (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 CreateGet<T>(propertyInfo);\n            }\n\n            if (memberInfo is FieldInfo fieldInfo)\n            {\n                return CreateGet<T>(fieldInfo);\n            }\n\n            throw new Exception(\"Could not create getter for {0}.\".FormatWith(CultureInfo.InvariantCulture, memberInfo));\n        }\n\n        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n        public Action<T, object?> CreateSet<T>(MemberInfo memberInfo)\n        {\n            if (memberInfo is PropertyInfo propertyInfo)\n            {","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Utilities/ReflectionDelegateFactory.cs#L30-L66","documentation":"Thrown by ReflectionDelegateFactory.CreateGet(MemberInfo) when a PropertyInfo's PropertyType.IsByRef is true — the property returns a managed pointer (C# 7 `ref` return). Json.NET's getter delegates return `object`, and a ByRef value cannot be boxed into the delegate pipeline, so it refuses outright. This is a hard design limitation of the reflection delegate factory, not a configuration problem.","triggerScenarios":"Serializing a type that exposes a property declared `public ref int X => ref _field;` (C# 7+ ref returns). Any code path that calls CreateGet on such a PropertyInfo triggers it — typically the serialization contract builder walking the type's properties.","commonSituations":"Interop with span-like wrappers or array-backed value-type mutability patterns that expose ref returns, performance-critical code reusing ref-returning properties in a serialization graph, or porting C# 7 code that wasn't designed for serialization.","solutions":["Replace the ref-returning property with a regular value-returning property used for serialization.","Annotate the ref-returning property with [JsonIgnore].","Project the type into a plain serializable DTO before serializing (e.g. via LINQ Select)."],"exampleFix":"// before\nprivate int _x;\npublic ref int X => ref _x;\n\n// after\nprivate int _x;\n[JsonIgnore]\npublic ref int X => ref _x;\npublic int XValue => _x;","handlingStrategy":"validation","validationCode":"// Exclude ref-returning properties from serialization candidates.\nbool IsSerializableProperty(PropertyInfo p) => !p.PropertyType.IsByRef;","typeGuard":"static bool IsNotByRefProperty(PropertyInfo p) => !p.PropertyType.IsByRef;","tryCatchPattern":null,"preventionTips":["Keep ref-returning properties out of your serialization graph; expose a plain value property alongside them.","Annotate ref-returning properties with [JsonIgnore].","Prefer projecting types into plain DTOs before serializing."],"tags":["reflection","serialization","byref","csharp7","property"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}