Powershell: Feature Request: Invoke Generic methods through PSMethodInfo

Created on 16 Apr 2020  路  5Comments  路  Source: PowerShell/PowerShell

Summary of the new feature/enhancement

Currently you can easily invoke regular methods through MethodInfo

$obj.SomeMethod.Invoke($arg1, $arg2) 

Probably it's not a particularly useful for the end users, but this approach might be extremely useful in case if you deal with generic methods. Would it be hard to implement something like:

$obj.SomeMethod.InvokeGeneric([SomeType], $arg1, $arg2))

It's a PITA to deal with generics at this point. There is some issues opened a while ago, and it looks like it's too much work to bring generics same way as it's implemented in C#. The above interface might be simple/straight forward to use and implement(hopefully).

Issue-Enhancement Resolution-Duplicate

All 5 comments

This would _most likely_ be a change in .NET Core itself rather than PowerShell, I would think? I _guess_ we could handle it with PSMethodInfo? 馃

If we're going to do something like this, we may as well just implement proper generic method handling, despite that probably being a little more complex to do properly.

Please see the already green-lit #5146

Thinking on this further... I have two thoughts.

  1. Might be a good enhancement that just makes it easier in the interim and also provides a pretty nice way to eventually implement the feature properly later on.
  2. Given that some methods take _multiple_ type parameters, we might run into some issues trying to work with the more complex methods. Perhaps the first argument can take Type[] rather than just Type for those ones.

@mikeTWC1984 and @vexx32, I see you were both part of the original discussion in #5146, which is awaiting implementation - of basically the syntax proposed here - by someone from the community. @vexx32, it even includes your multiple-types suggestions: https://github.com/PowerShell/PowerShell/issues/5146#issuecomment-339010927

Unless I'm missing something: isn't this issue a duplicate?

That's a good point. I'd missed that this suggestion was already mentioned in the other issue.

I'll close this as duplicate for now. @mikeTWC1984 if you have more to add to the discussion in #5146, please feel free to add to that issue. 馃檪

Was this page helpful?
0 / 5 - 0 ratings