Skip to content

Commit a36986a

Browse files
author
FriedrichWeinmann
committed
new command
1 parent 578403d commit a36986a

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

PSModuleDevelopment/PSModuleDevelopment.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
FunctionsToExport = @(
6565
'Expand-PSMDTypeName',
6666
'Find-PSMDFileContent',
67+
'Find-PSMDType',
6768
'Get-PSMDAssembly',
6869
'Get-PSMDConstructor',
6970
'Get-PSMDHelpEx',
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,111 @@
11
function 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

Comments
 (0)