11function Find-PSMDType
22{
3+ <#
4+ . SYNOPSIS
5+ Searches assemblies for types.
6+
7+ . DESCRIPTION
8+ This function searches the currently imported assemblies for a specific type.
9+ It is not inherently limited to public types however, and can search interna just as well.
10+
11+ Can be used to scan for dependencies, to figure out what libraries one needs for a given type and what dependencies exist.
12+
13+ . PARAMETER Name
14+ Default: "*"
15+ The name of the type to search for.
16+ Accepts wildcards.
17+
18+ . PARAMETER FullName
19+ Default: "*"
20+ The FullName of the type to search for.
21+ Accepts wildcards.
22+
23+ . PARAMETER Assembly
24+ Default: (Get-PSMDAssembly)
25+ The assemblies to search. By default, all loaded assemblies are searched.
26+
27+ . PARAMETER Public
28+ Whether the type to find must be public.
29+
30+ . PARAMETER Enum
31+ Whether the type to find must be an enumeration.
32+
33+ . PARAMETER Implements
34+ Whether the type to find must implement this interface
35+
36+ . PARAMETER InheritsFrom
37+ The type must directly inherit from this type.
38+ Accepts wildcards.
39+
40+ . EXAMPLE
41+ Find-PSMDType -Name "*String*"
42+
43+ Finds all types whose name includes the word "String"
44+ (This will be quite a few)
45+
46+ . EXAMPLE
47+ Find-PSMDType -InheritsFrom System.Management.Automation.Runspaces.Runspace
48+
49+ Finds all types that inherit from the Runspace class
50+ #>
351 [CmdletBinding ()]
452 Param (
53+ [string ]
54+ $Name = " *" ,
555
56+ [string ]
57+ $FullName = " *" ,
58+
59+ [Parameter (ValueFromPipeline = $true )]
60+ [System.Reflection.Assembly []]
61+ $Assembly = (Get-PSMDAssembly ),
62+
63+ [switch ]
64+ $Public ,
65+
66+ [switch ]
67+ $Enum ,
68+
69+ [string ]
70+ $Implements ,
71+
72+ [string ]
73+ $InheritsFrom
674 )
75+
76+ begin
77+ {
78+ $boundEnum = Test-PSFParameterBinding - ParameterName Enum
79+ $boundPublic = Test-PSFParameterBinding - ParameterName Public
80+ }
81+ process
82+ {
83+ foreach ($item in $Assembly )
84+ {
85+ if ($boundPublic )
86+ {
87+ if ($Public ) { $types = $item.ExportedTypes }
88+ else { $types = $item.GetTypes () | Where-Object IsPublic -EQ $false }
89+ }
90+ else
91+ {
92+ $types = $item.GetTypes ()
93+ }
94+
95+ foreach ($type in $types )
96+ {
97+ if ($type.Name -notlike $Name ) { continue }
98+ if ($type.FullName -notlike $FullName ) { continue }
99+ if ($Implements -and ($type.ImplementedInterfaces.Name -notcontains $Implements )) { continue }
100+ if ($boundEnum -and ($Enum -ne $type.IsEnum )) { continue }
101+ if ($InheritsFrom -and ($type.BaseType.FullName -notlike $InheritsFrom )) { continue }
102+
103+ $type
104+ }
105+ }
106+ }
107+ end
108+ {
109+
110+ }
7111}
0 commit comments