Skip to content

Commit 65a6ec5

Browse files
committed
Generics in PowerShell
1 parent 8339b6a commit 65a6ec5

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

development/ps/generic.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Workaround to call to a generic method OfType<T>() in PowerShell, when PowerShell cannot figure out <T> from the context
2+
3+
# Method 1 - using OfType<T> method
4+
[System.Linq.Enumerable]::OfType[int](@(1, 2, 'a'))
5+
6+
# Method 2 - using reflection
7+
$method = [System.Linq.Enumerable].GetMethod('OfType')
8+
$genericMethod = $method.MakeGenericMethod([int])
9+
$genericMethod.Invoke($null, @(, @(1, 2, 'a')))
10+
11+
# Example of using Distinct method
12+
[System.Linq.Enumerable]::Distinct([int[]]@(1, 2, 3, 1, 2))

0 commit comments

Comments
 (0)