Created
November 6, 2015 20:20
-
-
Save vors/d6a70e5d3439e928e603 to your computer and use it in GitHub Desktop.
Workaround to call to a generic method OfType<T>() in PowerShell, when PowerShell cannot figure out <T> from the context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PowerShell can infer generic <T> for method calls for your, when it has a parameter of the same type <T> | |
# Example: Enumerable.Distinct<TSource>(IEnumerable<TSource>) | |
# TSource can be inferred from the argument | |
[System.Linq.Enumerable]::Distinct([int[]]@(1,2,3,1,2)) | |
# Let's say you want to call a generic method without ability to infer types. | |
# Example: Enumerable.OfType<TResult>() | |
# Idially you may expect syntax like this | |
# [System.Linq.Enumerable].OfType[int](@(,@(1,2,'a')) | |
# where you tell PowerShell type explicitly. | |
# Unfortunately, that doesn't work. | |
# But there is a work-around. | |
# You just need to construct MethodInfo instance yourself using reflection | |
$method = [System.Linq.Enumerable].GetMethod('OfType') | |
$m = $method.MakeGenericMethod([int]) | |
$m.Invoke($null, @(,@(1,2,'a'))) # @(,@(1,2,'a')), because Invoke() expects array of arguments, so we need to wrap sequence twice. |
Nitpick: small correction to ideal syntax:
[System.Linq.Enumerable]::OfType[int](@(1,2,'a'))
A little late to the party. Hopefully the beer is still cold.
This has only limited testing is PS 6.2.4, but seems to work.
It uses TypeData to add a method to all objects (like an extension method). You pass in an array of types, the method name, and any parameters the method takes. PowerShell intercepts the parameters it cares about from the scriptblock call, and dumps everything else into args. We use that to determine the correct method to call, by grabbing their types, and to make a generic like you described above. We then splat the args into the invoke. Voila.
Update-Typedata -TypeName Object -MemberName InvokeGeneric -MemberType ScriptMethod -Force -Value {
param(
[type[]] $types,
[string] $methodName
)
$invokeTypes = [Type[]]@($args |% { $_.GetType() })
$method = $this.GetType().GetMethod($methodName, $invokeTypes)
if ($null -eq $method) {
throw "Invalid method name '$methodName'"
}
$methodCall = $method.MakeGenericMethod($types)
$methodCall.Invoke($this, $args)
}
#Example Usage
$xlDoc.InvokeGeneric([WorkBookPart], 'GetPartsOfType')
cool :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very hacky