JamesNK/Newtonsoft.Json · error · InvalidOperationException

Could not create getter for {0}. ByRef return values are not

Error message

Could not create getter for {0}. ByRef return values are not supported.

What it means

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.

Source

Thrown at Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs:79

            catch (Exception ex)
            {
                throw new JsonSerializationException("Error setting value to '{0}' on '{1}'.".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), ex);
            }
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="target">The target to get the value from.</param>
        /// <returns>The value.</returns>
        public object? GetValue(object target)
        {
            try
            {
                // https://github.com/dotnet/corefx/issues/26053
                if (_memberInfo is PropertyInfo propertyInfo && propertyInfo.PropertyType.IsByRef)
                {
                    throw new InvalidOperationException("Could not create getter for {0}. ByRef return values are not supported.".FormatWith(CultureInfo.InvariantCulture, propertyInfo));
                }

                return ReflectionUtils.GetMemberValue(_memberInfo, target);
            }
            catch (Exception ex)
            {
                throw new JsonSerializationException("Error getting value from '{0}' on '{1}'.".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), ex);
            }
        }
    }
}

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Exclude the by-ref property from serialization with [JsonIgnore].
  2. Expose the underlying value through a normal (non-ref) property and serialize that instead.
  3. Use [JsonProperty] to redirect serialization to a different, non-by-ref member.
  4. If the property is compiler-generated and unexpected, mark the entire type [JsonObject] with explicit member serialization to opt out of fields/opt-in.

Example fix

// before: by-ref property breaks serialization
public unsafe struct Buffer {
    public ref byte Data; // IsByRef == true -> 234
}
JsonConvert.Serialize(buffer);
// after: project to a normal property
public class BufferDto {
    [JsonIgnore] public Buffer Inner;
    public byte Data => Inner.Data;
}
Defensive patterns

Strategy: validation

Validate before calling

foreach (var p in type.GetProperties()) if (p.PropertyType.IsByRef) throw new InvalidOperationException(p.Name + " is ByRef; mark [JsonIgnore]");

Type guard

static bool IsByRefProperty(PropertyInfo p) => p.PropertyType.IsByRef;

Try / catch

try { JsonConvert.SerializeObject(obj); }
catch (InvalidOperationException ex) when (ex.Message.Contains("ByRef return values are not supported")) {
    logger.Error(ex, "by-ref property cannot be serialized; add [JsonIgnore]."); throw;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/8515eb03f42bbe39. Report an issue: GitHub.