< Back

Invoke-GenericMethod

Mon Jan 13, 2020 8:09 pm

NAME Invoke-GenericMethod



SYNOPSIS

Invokes Generic methods on .NET Framework types





SYNTAX

Invoke-GenericMethod -InputObject <Object> -MethodName <String> -GenericType <Type[]> [-ArgumentList <Object[]>]

[<CommonParameters>]



Invoke-GenericMethod -Type <Type> -MethodName <String> -GenericType <Type[]> [-ArgumentList <Object[]>]

[<CommonParameters>]





DESCRIPTION

Allows the caller to invoke a Generic method on a .NET object or class with a single function call.

Invoke-GenericMethod handles identifying the proper method overload, parameters with default values, and to some

extent, the same type conversion behavior you expect when calling a normal .NET Framework method from PowerShell.





PARAMETERS

-InputObject <Object>

The object on which to invoke an instance generic method.



Required? true

Position? named

Default value

Accept pipeline input? true (ByValue)

Accept wildcard characters? false



-Type <Type>

The .NET class on which to invoke a static generic method.



Required? true

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-MethodName <String>

The name of the generic method to be invoked.



Required? true

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-GenericType <Type[]>

One or more types which are specified when calling the generic method. For example, if a method's signature

is "string MethodName<T>();", and you want T to be a String, then you would pass "string" or ([string]) to the

Type parameter of Invoke-GenericMethod.



Required? true

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-ArgumentList <Object[]>

The arguments to be passed on to the generic method when it is invoked. The order of the arguments must match

that of the .NET method's signature; named parameters are not currently supported.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS

System.Object





OUTPUTS

System.Object





NOTES





Known issues:



Ref / Out parameters and [PSReference] objects are currently not working properly, and I don't think there's a

way to fix that from within PowerShell. I'll have to expand on the

PSGenericTypes.MethodInvoker.InvokeMethod() C# code to account for that.



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>Invoke-GenericMethod -InputObject $someObject -MethodName SomeMethodName -GenericType string -ArgumentList

$arg1,$arg2,$arg3



Invokes a generic method on an object. The signature of this method would be something like this (containing 3

arguments and a single Generic type argument): object SomeMethodName<T>(object arg1, object arg2, object arg3);









-------------------------- EXAMPLE 2 --------------------------



PS C:\\>$someObject | Invoke-GenericMethod -MethodName SomeMethodName -GenericType string -ArgumentList

$arg1,$arg2,$arg3



Same as example 1, except $someObject is passed to the function via the pipeline.









-------------------------- EXAMPLE 3 --------------------------



PS C:\\>Invoke-GenericMethod -Type SomeClass -MethodName SomeMethodName -GenericType string,int -ArgumentList

$arg1,$arg2,$arg3



Invokes a static generic method on a class. The signature of this method would be something like this (containing

3 arguments and two Generic type arguments): static object SomeMethodName<T1,T2> (object arg1, object arg2,

object arg3);











RELATED LINKS