Skip to content

System.Text.Json does not support constructors with byref parameters. #46088

Closed

Description

@mattchidley

Description

I have a List of objects whose constructors take 'in' parameters of DateTime and TimeSpan.

public class TestClass
    {
        public TestClass(in DateTime dateTime, in TimeSpan timeSpan)
        {
            DateTime = dateTime;
            TimeSpan = timeSpan;
        }

        public DateTime DateTime { get; set; }

        public TimeSpan TimeSpan { get; set; }
    }

When serializing a list of these objects, I am seeing the exception in the title being thrown:

image

image

This is specifically being thrown at IEnumerableDefaultConverter.cs line 255:

state.Current.DeclaredJsonPropertyInfo = state.Current.JsonClassInfo.ElementClassInfo!.PropertyInfoForClassInfo;

Unfortunately I have absolutely no clue what the issue is here. I'm hoping you might be able to help.

Activity

  1. added
    untriaged
    on Dec 15, 2020
  2. Dotnet-GitSync-Bot commented on Dec 15, 2020

    @Dotnet-GitSync-Bot
    Collaborator

    I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

  3. GrabYourPitchforks commented on Dec 16, 2020

    @GrabYourPitchforks
    Member

    Looks like System.Text.Json doesn't understand byrefs as ctor arguments? You can work around this for now by removing the in keyword from your ctor parameters.

  4. added this to the 6.0.0 milestone on Jan 25, 2021
  5. added and removed
    untriaged
    on Jan 25, 2021
  6. self-assigned this
    on Jan 25, 2021
  7. modified the milestones: 6.0.0, Future on Apr 9, 2021
  8. removed their assignment
    on Apr 9, 2021
  9. added
    help wanted
    on Apr 9, 2021
  10. ilanb1996 commented on Apr 18, 2021

    @ilanb1996

    Hi @GrabYourPitchforks
    Im looking for an issue to take as my fist contribution and stumbled upon this issue.
    Do you think its ok if i take it on me?

  11. GrabYourPitchforks commented on Apr 18, 2021

    @GrabYourPitchforks
    Member

    @layomia - thoughts?

  12. layomia commented on Apr 19, 2021

    @layomia
    Contributor

    @GrabYourPitchforks, @ilanb1996 sounds good, thanks! I'll assign the issue to you - please tag me with any questions.

  13. 19 remaining items

  14. ViktorHofer commented on Jun 14, 2021

    @ViktorHofer
  15. eiriktsarpalis commented on Oct 22, 2021

    @eiriktsarpalis
    Member

    Out of curiosity, what should be our expected approach to fixing this? I presume the error occurs when attempting to instantiate SmallObjectWithParameterizedConstructorConverter<> with DateTime& as a type parameter. Presumably we'd have to fall back to LargeObjectWithParameterizedConstructorConverterWithReflection?

  16. changed the title [-]System.Text.Json argument exception: The type 'System.DateTime&' may not be used as a type argument.[/-] [+]System.Text.Json does not support constructors with byref parameters.[/+] on Aug 2, 2022
  17. modified the milestones: Future, 8.0.0 on Aug 2, 2022
  18. eiriktsarpalis commented on Aug 5, 2022

    @eiriktsarpalis
    Member

    Once we address this issue we should make sure to re-enable the following disabled test:

    [Fact]
    [ActiveIssue("https://github.com/dotnet/runtime/issues/34779")]
    public async Task BindingBetweenRefProps()
    {
    string json = @"{""NameRef"":""John""}";
    await Serializer.DeserializeWrapper<TypeWith_RefStringProp_ParamCtor>(json);
    }

  19. modified the milestones: 8.0.0, Future on Sep 29, 2022
  20. added
    in-pr
    on Jan 6, 2026
  21. added a commit that references this issue on Mar 20, 2026
    9096b69
  22. locked and limited conversation to collaborators on Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-System.Text.Jsonbugin-pr

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions