Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
@mklement0

Description

@mklement0
Issue body actions

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

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.