Skip to content

CodeMethod invoke fails with dot syntax but works with Foreach-Object -MemberName #4826

Closed

Description

Steps to reproduce

using namespace System.Management.Automation
class Y{
    [int] $Id
}

class X{    
	static [void] OpenUri([psobject] $obj) {
		$id = $obj.Id
		start-process "http://baseuri/$id"
	}

    static [void] UpdateTypeData(){
        $td = [Runspaces.Typedata]::new([Y])		
		$td.Members.Add("OpenUri",
			[Runspaces.CodeMethodData]::new("OpenUri", [X]::GetMethodInfo([X], "OpenUri")));
		Update-TypeData -TypeData $td -Force
    }
    
    hidden static [Reflection.MethodInfo] GetMethodInfo([Type] $type, [string] $method) {
		return $type.GetMethod($method, [Reflection.BindingFlags]::Static -bor [Reflection.BindingFlags]::Public -bor [Reflection.BindingFlags]::IgnoreCase);
	}
}
[X]::UpdateTypeData()

$y = [Y]::new()

#works
$y | % OpenUri
#fails with No coercion operator is defined between types 'System.Void' and 'System.Object'.
$y.OpenUri()

Expected behavior

Same behavior invoking method with dot syntax as when invoked via Foreach-Object -MemberName

Actual behavior

fails with

No coercion operator is defined between types 'System.Void' and 'System.Object'.
At line:1 char:1
+ $y.OpenUri()
PS C:\Program Files\PowerShell\6.0.0-beta.6> $y | gm

   TypeName: Y

Name        MemberType Definition
----        ---------- ----------
OpenUri     CodeMethod static void OpenUri(psobject obj)

Environment data

All versions (beta 6 and win 5.1)

Activity

  1. powercode commented on Sep 13, 2017

    @powercode
    CollaboratorAuthor

    It works if the OpenUri method is returning something, i.e. is not a [void] return.

  2. added
    WG-Language
    on Sep 13, 2017
  3. added
    Issue-Bug
    Up-for-Grabs
    on Sep 14, 2017
  4. lzybkr commented on Sep 14, 2017

    @lzybkr
    Contributor

    This is probably an easy fix for someone interested in dynamic sites and code gen.

    Taking the original repro, I've added an explanation of how the foreach example works - it's using a PSMethod which goes through reflection:

    # works
    $y.OpenUri.Invoke()
    #works because the above works
    $y | % OpenUri
    #fails with No coercion operator is defined between types 'System.Void' and 'System.Object'.
    $y.OpenUri()
    
  5. powercode commented on Sep 16, 2017

    @powercode
    CollaboratorAuthor

    PowerShellTeam I'll take a stab at it.

  6. added a commit that references this issue on Sep 20, 2017
    916ef56
  7. added a commit that references this issue on Aug 6, 2026
    5aeef45
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-BugResolution-FixedUp-for-GrabsWG-Language

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions