For example I have a .NET object $m
with the following method overloads:
PS C:\Users\Me> $m.GetBody
OverloadDefinitions
-------------------
T GetBody[T]()
T GetBody[T](System.Runtime.Serialization.XmlObjectSerializer serializer)
If I try to invoke the parameterless method I get:
PS C:\Users\Me> $m.GetBody()
Cannot find an overload for "GetBody" and the argument count: "0".
At line:1 char:1
+ $m.GetBody()
+ ~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
I understand PowerShell v3.0 is supposed to work more easily with generics. Obviously I need to tell it somehow what type I want returned but I cannot figure out the syntax.
Calling a generic method on an object instance:
Calling a static generic method (see also Calling generic static method in PowerShell):
Note that you will encounter an
AmbiguousMatchException
when there is also a non-generic version of the method (see How do I distinguish between generic and non generic signatures using GetMethod in .NET?). UseGetMethods()
then:(Mind that there could be more than one method that match the above filter, so make sure to adjust it to find the one you need.)
Hint: You can write complex generic type literals like this (see Generic type of Generic Type in Powershell):
To call a (parameterless) generic method with overloads from Powershell v3, as shown in the OP example, use the script Invoke-GenericMethod.ps1 from the reference provided by @Chad Carisch, Invoking Generic Methods on Non-Generic Classes in PowerShell.
It should look something like
This is a verified working code sample that I am using:
Here is an alternate script that I haven't tried but is more recent and being actively maintained, Invoke Generic Methods from PowerShell.
marsze's helpful answer contains great general information about calling generic methods, but let me address the aspect of calling a parameter-less one specifically, as asked:
As hinted at in the question:
As of Windows PowerShell v5.1 / PowerShell Core v6.1.0, PowerShell has no syntax that would allow you to specify the type explicitly in this scenario.
However, there is a suggestion on GitHub to enhance the syntax in PowerShell Core that has been green-lighted in principle, but is awaiting implementation by the community.
For now, reflection must be used:
.GetMethod('GetBody', [type[]] @())
unambiguously finds the parameter-less overload of.GetBody()
, due to passing in an empty array of parameter types..MakeGenericMethod([decimal])
instantiates the method with example type[decimal]
..Invoke($m, @())
then invokes the type-instantiated method on input object ($m
) with no arguments (@()
, the empty array).It looks like you are trying to invoke a generic method.
In powershell this can be done by:
See this wonderful blog post for more info and additional examples.
For your example you could try:
This can be simplified by doing:
This has been testing in powershell 2 and powershell 3.
If you had a more detailed example of how you came across this generic method I would be able to give more details. I have yet to see any microsoft cmdlets return anything that give you generic methods. The only time this comes up is when custom objects or methods from c# or vb.net are used.
To use this without any parameters you can use Invoke with just the first parameter. $gMethod.Invoke($nonGenericClass)