Skip to content

Commit ac677c3

Browse files
author
Friedrich Weinmann
committed
Added filter by attribute
Also fixed bad assembly exceptions
1 parent b3bbbcd commit ac677c3

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

PSModuleDevelopment/functions/assembly/Find-PSMDType.ps1

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
The type must directly inherit from this type.
3838
Accepts wildcards.
3939
40+
.PARAMETER Attribute
41+
The type must have this attribute assigned.
42+
Accepts wildcards.
43+
4044
.EXAMPLE
4145
Find-PSMDType -Name "*String*"
4246
@@ -70,7 +74,10 @@
7074
$Implements,
7175

7276
[string]
73-
$InheritsFrom
77+
$InheritsFrom,
78+
79+
[string]
80+
$Attribute
7481
)
7582

7683
begin
@@ -85,11 +92,18 @@
8592
if ($boundPublic)
8693
{
8794
if ($Public) { $types = $item.ExportedTypes }
88-
else { $types = $item.GetTypes() | Where-Object IsPublic -EQ $false }
95+
else
96+
{
97+
# Empty Assemblies will error on this, which is not really an issue and can be safely ignored
98+
try { $types = $item.GetTypes() | Where-Object IsPublic -EQ $false }
99+
catch { Write-PSFMessage -Message "Failed to enumerate types on $item" -Level InternalComment -Tag 'fail','assembly','type','enumerate' -ErrorRecord $_ }
100+
}
89101
}
90102
else
91103
{
92-
$types = $item.GetTypes()
104+
# Empty Assemblies will error on this, which is not really an issue and can be safely ignored
105+
try { $types = $item.GetTypes() }
106+
catch { Write-PSFMessage -Message "Failed to enumerate types on $item" -Level InternalComment -Tag 'fail', 'assembly', 'type', 'enumerate' -ErrorRecord $_ }
93107
}
94108

95109
foreach ($type in $types)
@@ -99,6 +113,7 @@
99113
if ($Implements -and ($type.ImplementedInterfaces.Name -notcontains $Implements)) { continue }
100114
if ($boundEnum -and ($Enum -ne $type.IsEnum)) { continue }
101115
if ($InheritsFrom -and ($type.BaseType.FullName -notlike $InheritsFrom)) { continue }
116+
if ($Attribute -and ($type.CustomAttributes.AttributeType.Name -notlike $Attribute)) { continue }
102117

103118
$type
104119
}

0 commit comments

Comments
 (0)