-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Support constructors with byref parameters (in/ref/out) in System.Text.Json #122950
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
base: main
Are you sure you want to change the base?
Changes from 10 commits
0cd298b
185a1fe
55d3b03
eec5b88
34a2088
316c65d
2509f8c
c3a2b48
7d164fd
4f8089e
7de1ab7
45e9b72
f544feb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,7 +62,14 @@ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializer | |
| foreach (ParameterInfo parameter in parameters) | ||
| { | ||
| // Every argument must be of supported type. | ||
| JsonTypeInfo.ValidateType(parameter.ParameterType); | ||
| // For byref parameters (in/ref/out), validate the underlying element type. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot, are there tests covering in, out, and ref?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests currently cover
The
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot can you add tests for ref and out that at least lock in the current behaviors, rather than leaking them totally untested?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in 316c65d. The tests lock in current behavior:
|
||
| Type parameterType = parameter.ParameterType; | ||
| if (parameterType.IsByRef) | ||
| { | ||
| parameterType = parameterType.GetElementType()!; | ||
| } | ||
|
|
||
| JsonTypeInfo.ValidateType(parameterType); | ||
| } | ||
|
|
||
| if (parameterCount <= JsonConstants.UnboxedParameterCountThreshold) | ||
|
|
@@ -75,7 +82,15 @@ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializer | |
| { | ||
| if (i < parameterCount) | ||
| { | ||
| typeArguments[i + 1] = parameters[i].ParameterType; | ||
| // For byref parameters (in/ref/out), use the underlying element type | ||
| // since byref types cannot be used as generic type arguments. | ||
| Type parameterType = parameters[i].ParameterType; | ||
| if (parameterType.IsByRef) | ||
| { | ||
| parameterType = parameterType.GetElementType()!; | ||
| } | ||
|
|
||
| typeArguments[i + 1] = parameterType; | ||
| } | ||
| else | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot rather than hardcoding numeric values, consider defining constants only for the RefKinds not available in the current version of Roslyn.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 7de1ab7. Now using
RefKind.RefandRefKind.Outdirectly, with onlyRefKindRefReadOnlyParameter(value 4) defined as a constant since it was added in Roslyn 4.4.