Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Tracked by #71944
mattchidley opened this issue Dec 15, 2020 · 25 comments
Open
Tracked by #71944

Comments

@mattchidley
Copy link

mattchidley commented Dec 15, 2020

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.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Dec 15, 2020
@Dotnet-GitSync-Bot
Copy link
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.

@GrabYourPitchforks
Copy link
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.

@layomia layomia added this to the 6.0.0 milestone Jan 25, 2021
@layomia layomia added bug and removed untriaged New issue has not been triaged by the area owner labels Jan 25, 2021
@layomia layomia self-assigned this Jan 25, 2021
@layomia layomia modified the milestones: 6.0.0, Future Apr 9, 2021
@layomia layomia removed their assignment Apr 9, 2021
@layomia layomia added the help wanted [up-for-grabs] Good issue for external contributors label Apr 9, 2021
@ilanb1996
Copy link

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?

@GrabYourPitchforks
Copy link
Member

@layomia - thoughts?

@layomia
Copy link
Contributor

layomia commented Apr 19, 2021

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

@layomia layomia removed the help wanted [up-for-grabs] Good issue for external contributors label Apr 19, 2021
@ilanb1996

This comment has been minimized.

@GrabYourPitchforks

This comment has been minimized.

@ilanb1996

This comment has been minimized.

@ilanb1996

This comment has been minimized.

@GrabYourPitchforks

This comment has been minimized.

@ilanb1996

This comment has been minimized.

@ilanb1996

This comment has been minimized.

@ilanb1996

This comment has been minimized.

@GrabYourPitchforks

This comment has been minimized.

@layomia

This comment has been minimized.

@eerhardt

This comment has been minimized.

@ViktorHofer

This comment has been minimized.

@ilanb1996

This comment has been minimized.

@ViktorHofer

This comment has been minimized.

@ilanb1996

This comment has been minimized.

@ViktorHofer

This comment has been minimized.

@ilanb1996

This comment has been minimized.

@ViktorHofer

This comment has been minimized.

@eiriktsarpalis
Copy link
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?

@eiriktsarpalis eiriktsarpalis 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. Aug 2, 2022
@eiriktsarpalis eiriktsarpalis modified the milestones: Future, 8.0.0 Aug 2, 2022
@eiriktsarpalis
Copy link
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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants