helpers/Test-GenericSubclassOf.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#Requires -Version 3.0

<#
  Test-GenericSubclassOf.ps1
 
  Copyright (c) 2017 karamem0
 
  This software is released under the MIT License.
  https://github.com/karamem0/SPClient/blob/master/LICENSE
#>


function Test-GenericSubclassOf {

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [type]
        $InputType,
        [Parameter(Mandatory = $true)]
        [type]
        $TestType
    )

    process {
        while ($InputType -ne $null -and $InputType -ne [object]) {
            $Type = $InputType
            if ($Type.IsGenericType) {
                $Type = $Type.GetGenericTypeDefinition()
            }
            if ($Type -eq $TestType) {
                Write-Output $true
                return
            }
            $InputType = $InputType.BaseType
        }
        Write-Output $false
    }

}