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
MethodInfo Invoke with [Optional] argument as Type.Missing fails #100322
Comments
Tagging subscribers to this area: @dotnet/area-system-reflection |
This is by design; see the test Invoke_OptionalParameterUnassingableFromMissing_WithMissingValue_ThrowsArgumentException. where the parameter type is Also verified previous versions (v7, v8) are consistent. |
Thanks @steveharter for replying, so is the solution here to just provide |
Oh and the linked issue for that test seems to be unrelated mono/mono#15025 so not sure what the reason behind this behaviour is. |
Description
I'm trying to invoke a method that contains an OptionalAttribute without a default value set using reflection but it fails when using
Type.Missing
withReproduction Steps
Expected behavior
Actual behavior
Regression?
It does not look like it, the same example fails on .NET Framework 4.8.
Known Workarounds
The workaround is to pass in
null
(I'm not sure if that works in all scenarios or not).Configuration
net8.0
All OS
Tested on x64
I don't believe this is specific to this configuration.
Other information
This is opened in response to PowerShell/PowerShell#21372. PowerShell is using an ExpressionTree to invoke the method. It uses the ParameterInfo.DefaultValue for arguments not specified in PowerShell which for
TestMethod3()
is Missing.I've got a patch for PowerShell that uses
Expression.Default(parameters[0].ParameterType);
which solves this issue but it seems like this might be a bug in .NET that should be fixed here. If not it would be great to understand more about why the current behaviour fails and if there are other cases wereParameterInfo.DefaultType
might also beSystem.Reflection.Missing
.The text was updated successfully, but these errors were encountered: