Skip to content

PowerShell fails to call .NET method that that has an optional IntPtr parameter whose default value is Int32 0  #19429

Open

Description

Prerequisites

Steps to reproduce

# !! BROKEN: 
#   * `default(IntPtr)` (or `default`) are converted to System.Int32(!) 0  on compilation (the same happens in regular .NET SDK probject)
#   * C# code can call such a method just fine, but PowerShell complains.
#  * Workaround: use `new IntPtr()` as the default value.
(Add-Type -PassThru -Name Foo1 -MemberDefinition @'
    public static void Bar(IntPtr p = default(IntPtr)) { }
'@)::Bar()

Expected behavior

The method calls should succeed (it produces no output).

Actual behavior

The method call fails with the error below.

Error details

Exception             : 
    Type       : System.ArgumentException
    Message    : Expression of type 'System.Int32' cannot be used for parameter of type 'System.IntPtr' of method 'Void Bar(IntPtr)' (Parameter 'arg0')
    ParamName  : arg0
    TargetSite : 
        Name          : ValidateOneArgument
        DeclaringType : System.Dynamic.Utils.ExpressionUtils, System.Linq.Expressions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
        MemberType    : Method
        Module        : System.Linq.Expressions.dll
    Data       : System.Collections.ListDictionaryInternal
    Source     : System.Linq.Expressions
    HResult    : -2147024809
    StackTrace : 
   at System.Dynamic.Utils.ExpressionUtils.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arguments, ParameterInfo pi, String methodParamName, String argumentParamName, Int32 index)
   at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression arg0)
   at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
   at System.Management.Automation.Language.PSInvokeMemberBinder.InvokeMethod(MethodBase mi, DynamicMetaObject target, DynamicMetaObject[] args, Boolean expandParameters, MethodInvocationType invocationType)
   at System.Management.Automation.Language.PSInvokeMemberBinder.InvokeDotNetMethod(CallInfo callInfo, String name, PSMethodInvocationConstraints psMethodInvocationConstraints, MethodInvocationType methodInvocationType, DynamicMetaObject target, DynamicMetaObject[] args, BindingRestrictions restrictions, MethodInformation[] mi, Type errorExceptionType)
   at System.Management.Automation.Language.PSInvokeMemberBinder.FallbackInvokeMember(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
   at System.Management.Automation.PSObject.PSDynamicMetaObject.BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
   at System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args, ReadOnlyCollection`1 parameters, LabelTarget returnLabel)
   at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite`1 site, Object[] args)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at System.Management.Automation.Interpreter.DynamicInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
CategoryInfo          : OperationStopped: (:) [], ArgumentException
FullyQualifiedErrorId : System.ArgumentException
InvocationInfo        : 
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 2
    Line             : (Add-Type -PassThru -Name Foo1 -MemberDefinition @'
                       
    Statement        : (Add-Type -PassThru -Name Foo1 -MemberDefinition @'
                       public static void Bar(IntPtr p = default(IntPtr)) { }
                       '@)::Bar()
    PositionMessage  : At line:1 char:1
                       + (Add-Type -PassThru -Name Foo1 -MemberDefinition @'
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

PowerShell Core 7.4.0-preview.2

Visuals

No response

Activity

  1. iSazonov commented on Mar 30, 2023

    @iSazonov
    Collaborator

    Is the issue in 7.3?

  2. changed the title [-]Add-Type unexpectedly converts a `default(IntPtr)` default parameter value into `System.Int32`[/-] [+]PowerShell fails to call .NET method that that has an optional IntPtr parameter whose default value is Int32 0 [/+] on Mar 30, 2023
  3. mklement0 commented on Mar 30, 2023

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov), the issue was framed incorrectly - please see the updated title and initial post.

    It is a problem with method invocation, irrespective of whether the code was compiled with Add-Type or via a .NET SDK project.

    The problem also exists in 7.3.3, but not in 7.2.10, and also not in Windows PowerShell.

  4. added this to the 7.4-Consider milestone on Mar 30, 2023
  5. SeeminglyScience commented on Oct 16, 2023

    @SeeminglyScience
    Contributor

    The Engine WG discussed this and agree it's a bug that should be fixed.

  6. added
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    and removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Oct 16, 2023
  7. microsoft-github-policy-service commented on Apr 14, 2024

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

  8. added
    KeepOpenThe bot will ignore these and not auto-close
    and removed
    Resolution-No ActivityIssue has had no activity for 6 months or more
    on Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productKeepOpenThe bot will ignore these and not auto-closeUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Engine-ParameterBinder

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions