Skip to content

Fix: using default value for nullable enum parameter throws ArgumentException #71388

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

Merged
merged 5 commits into from
Jul 5, 2022

Conversation

buyaa-n
Copy link
Contributor

@buyaa-n buyaa-n commented Jun 28, 2022

Fix: Type.Missing doesn't convert to nullable enum in Method.Invoke

The root cause is: ParameterInfo.DefaultValue returns the raw value if the enum is nullable. So the root cause is a known issue and we choose to not fix for compatibility reason:

// NOTE: Unlike in `TypeBuilder.SetConstantValue`, if `fieldType` describes
// a nullable enum type `Nullable<TEnum>`, we do not unpack it to `TEnum` to
// successfully enter this `if` clause. Default values of `TEnum?`-typed
// parameters have been reported as values of the underlying type, changing
// this now might be a breaking change.

Though for MethodInfo.Invoke we could parse the raw value into the corresponding Enum and avoid throwing ArgumentException

Fixes #70925

@ghost
Copy link

ghost commented Jun 28, 2022

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

Issue Details

Fix: Type.Missing doesn't convert to nullable enum in Method.Invoke

The root cause is: ParameterInfo.DefaultValue returns the raw value if the enum is nullable. So the root cause is a known issue and we choose to not fix for compatibility reason:

// NOTE: Unlike in `TypeBuilder.SetConstantValue`, if `fieldType` describes
// a nullable enum type `Nullable<TEnum>`, we do not unpack it to `TEnum` to
// successfully enter this `if` clause. Default values of `TEnum?`-typed
// parameters have been reported as values of the underlying type, changing
// this now might be a breaking change.

Though for MethodInfo.Invoke we could parse the raw value into the corresponding Enum and avoid throwing ArgumentException

Fixes #70925

Author: buyaa-n
Assignees: -
Labels:

area-System.Reflection

Milestone: -

@steveharter
Copy link
Contributor

Looks like some of the new tests are failing in NativeAOT. You likely will want an ActiveIssue there.

Copy link
Contributor

@steveharter steveharter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once CI green for NativeAOT

@steveharter
Copy link
Contributor

Apparently, the new tests work on Mono?

@jkotas
Copy link
Member

jkotas commented Jun 28, 2022

Looks like some of the new tests are failing in NativeAOT. You likely will want an ActiveIssue there.

Could you please look into fixing the failing tests as part of this PR?

@AaronRobinsonMSFT AaronRobinsonMSFT changed the title Fix: using default value for nullabel enum parameter throws ArgumentException Fix: using default value for nullable enum parameter throws ArgumentException Jun 28, 2022
@buyaa-n buyaa-n force-pushed the nullable-enum-default branch from 835b818 to 832bfdd Compare July 1, 2022 00:55
@buyaa-n buyaa-n merged commit 2170ea9 into dotnet:main Jul 5, 2022
@buyaa-n buyaa-n deleted the nullable-enum-default branch July 5, 2022 21:04
@ghost ghost locked as resolved and limited conversation to collaborators Aug 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type.Missing doesn't convert to nullable enum in Method.Invoke
5 participants