Skip to content

System.Text.Json: constructors mixing out and regular parameters fail to deserialize #137797

Description

@sebastienros

Description

Support for constructors with byref parameters (in/ref/out) was added in #122950 (closing #46088). Constructors that mix an out parameter with regular parameters fail to deserialize with the reflection-based serializer in two cases:

  1. Four or fewer parameters, default settings. Deserialization throws InvalidCastException.
  2. More than four parameters, when dynamic code is not supported (RuntimeFeature.IsDynamicCodeSupported == false, e.g. NativeAOT with reflection-based serialization enabled). Arguments are passed to the constructor shifted by one position.

Reproduction Steps

using System;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Serialization;

Console.WriteLine($"IsDynamicCodeSupported={RuntimeFeature.IsDynamicCodeSupported}");
Try<Large>("""{"A":1,"B":"two","C":3,"D":4,"E":5}""");
Try<Small>("""{"A":1,"B":"two"}""");

static void Try<T>(string json)
{
    try { Console.WriteLine($"{typeof(T).Name}: {JsonSerializer.Deserialize<T>(json)}"); }
    catch (Exception e) { Console.WriteLine($"{typeof(T).Name}: {e.GetType().Name}: {e.Message}"); }
}

public class Large
{
    public int A { get; } public string B { get; } public int C { get; } public int D { get; } public int E { get; }

    [JsonConstructor]
    public Large(int a, out int ignored, string b, int c, int d, int e) { ignored = 0; A = a; B = b; C = c; D = d; E = e; }

    public override string ToString() => $"A={A} B={B} C={C} D={D} E={E}";
}

public class Small
{
    public int A { get; } public string B { get; }

    [JsonConstructor]
    public Small(out int ignored, int a, string b) { ignored = 0; A = a; B = b; }

    public override string ToString() => $"A={A} B={B}";
}

Case 2 can be reproduced on CoreCLR without NativeAOT by adding this to the project file:

<ItemGroup>
  <RuntimeHostConfigurationOption Include="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported" Value="false" />
</ItemGroup>

It also reproduces in a real NativeAOT build (PublishAot=true, JsonSerializerIsReflectionEnabledByDefault=true, app assembly rooted for trimming).

Expected behavior

Both types deserialize:

Large: A=1 B=two C=3 D=4 E=5
Small: A=1 B=two

Actual behavior

Default settings (CoreCLR):

IsDynamicCodeSupported=True
Large: A=1 B=two C=3 D=4 E=5
Small: InvalidCastException: Unable to cast object of type 'System.Text.Json.Serialization.Metadata.JsonParameterInfo`1[System.Int32]' to type 'System.Text.Json.Serialization.Metadata.JsonParameterInfo`1[System.Object]'.

With IsDynamicCodeSupported=false on CoreCLR, and identically for Large in a NativeAOT build:

IsDynamicCodeSupported=False
Large: ArgumentException: Object of type 'System.String' cannot be converted to type 'System.Int32&'.

Regression?

No. Constructors with out parameters were not supported before #122950.

Known Workarounds

Use the source generator: both types deserialize correctly through a JsonSerializerContext, on CoreCLR and NativeAOT. Otherwise, avoid out parameters in the constructor used for deserialization.

Configuration

  • System.Text.Json 11.0.0.0 (in-box)
  • macOS arm64, .NET SDK 11.0.100 (CoreCLR runs)
  • Windows x64, .NET SDK 11.0.100-rc.3 (CoreCLR and NativeAOT runs)

Other information

Case 2. DefaultJsonTypeInfoResolver.PopulateParameterInfoValues creates no entry for out parameters, so the argument array holds only the non-out values. ReflectionEmitMemberAccessor.CreateParameterizedConstructor<T>(ConstructorInfo) maps array indices to constructor positions (argsIndices, -1 for out). ReflectionMemberAccessor.CreateParameterizedConstructor<T>(ConstructorInfo) does not; both of its branches pass the array positionally:

#if NET
    ConstructorInvoker invoker = ConstructorInvoker.Create(constructor);
    return arguments => (T)invoker.Invoke(arguments.AsSpan(0, parameterCount));
#else
    ...
    Array.Copy(arguments, 0, argsToPass, 0, parameterCount);

The non-.NET branch is what the netstandard2.0 asset uses on runtimes other than .NET Framework; running the netstandard2.0 build on .NET Framework is how this was first noticed (NonPublicCtors_WithByRefParameters fails there).

Case 1. Not investigated to root cause. The converter chosen for Small is SmallObjectWithParameterizedConstructorConverter<Small, object, int, string, object>: the typed argument slots follow constructor position (with object for the out slot), while the parameter metadata skips out entries, so slot 0 appears to receive the JsonParameterInfo<int> that belongs to a.

Test coverage. The existing tests cover constructors whose parameters are all out, and one five-parameter mixed constructor (PrivateCtorWithByRefParameters), which runs through the emit accessor. There is no mixed constructor with four or fewer parameters, and no mixed constructor run without dynamic code.

Activity

  1. added
    untriaged
    on Nov 18, 2026
  2. dotnet-policy-service commented on Nov 18, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-json
    See info in area-owners.md if you want to be subscribed.

  3. removed
    untriaged
    on Nov 25, 2026
  4. added a commit that references this issue on Nov 25, 2026
    c2e4466
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions