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

Fix up .NET method invocation with Optional arg #21387

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jborean93
Copy link
Collaborator

PR Summary

Fix .NET method invocation with an argument that has an [Optional] attribute but no default value.

Fixes: #21372

PR Context

While not common .NET has the ability to set an optional argument but no explicit value https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments#optional-arguments

You can also declare optional parameters by using the .NET OptionalAttribute class. OptionalAttribute parameters do not require a default value. However, if a default value is desired, take a look at DefaultParameterValueAttribute class.

The docs for F# even recommend this attribute when it comes to F# assemblies that could be used in C# and other .NET languages https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/parameters-and-arguments#optional-parameters

For the purposes of C# and Visual Basic interop you can use the attributes [<Optional; DefaultParameterValue<(...)>] in F#, so that callers will see an argument as optional. This is equivalent to defining the argument as optional in C# as in MyMethod(int i = 3).

When it comes to the compiled assembly the ParameterInfo.DefaultValue for an argument with only [Optional] is System.Reflection.Missing but this value cannot be passed as an argument unless the type is object because it cannot be casted to the argument type. Instead using null or the default value for that type should be used.

This is a good candidate to backport.

PR Checklist

Fix .NET method invocation with an argument that has an [Optional]
attribute but no default value.
@iSazonov iSazonov added CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log BackPort-7.4.x-Consider labels Mar 28, 2024
// If the method contains just [Optional] without a default value set then we cannot use
// Type.Missing as a placeholder. Instead we use the default value for that type. Only
// exception to this rule is when the parameter type is object.
argExprs[i] = Expression.Default(parameterType);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please reword the comment.
It seems your comment in the PR description is more clear about behavior with object

When it comes to the compiled assembly the ParameterInfo.DefaultValue for an argument with only [Optional] is System.Reflection.Missing but this value cannot be passed as an argument unless the type is object because it cannot be casted to the argument type. Instead using null or the default value for that type should be used.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've updated the comment, please let me know if there's any further improvements or clarifications it should have.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks!

Please remove unrelated style and formatting changes. Our docs ask to pull separate PRs such changes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah sorry, that last host I made this change on was set to format on save, will undo those.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

@jborean93
Copy link
Collaborator Author

Thanks for the review @iSazonov

@SeeminglyScience SeeminglyScience added CommunityDay-Small A small PR that the PS team has identified to prioritize to review WG-Reviewed A Working Group has reviewed this and made a recommendation labels Apr 1, 2024
@SeeminglyScience
Copy link
Collaborator

The Engine WG discussed this and agree with the change and it is ready to be reviewed

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Apr 9, 2024
Copy link
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BackPort-7.4.x-Consider CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log CommunityDay-Small A small PR that the PS team has identified to prioritize to review Review - Needed The PR is being reviewed WG-Reviewed A Working Group has reviewed this and made a recommendation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

F#-based library (Deedle) fails in PS
3 participants