Skip to content

Instantly share code, notes, and snippets.

@vors
Created November 6, 2015 20:20
Show Gist options
  • Save vors/d6a70e5d3439e928e603 to your computer and use it in GitHub Desktop.
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
# 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.
@vors
Copy link
Author

vors commented Feb 7, 2020

cool :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment