Skip to content

Commit f6aa086

Browse files
committed
Enhance enum type resolution in SqlResourceBase class to handle ReflectionTypeLoadException
1 parent cf36ba7 commit f6aa086

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

source/Classes/011.SqlResourceBase.ps1

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,41 @@ class SqlResourceBase : ResourceBase
194194

195195
if (-not $enumType)
196196
{
197-
# Try loading from loaded assemblies if direct resolution fails.
198-
$enumType = [System.AppDomain]::CurrentDomain.GetAssemblies().GetTypes() |
199-
Where-Object -FilterScript { $_.FullName -eq $fullTypeName } |
200-
Select-Object -First 1
197+
<#
198+
Try loading from loaded assemblies if direct resolution fails.
199+
Must iterate through assemblies individually and handle
200+
ReflectionTypeLoadException because some assemblies contain
201+
types that cannot be loaded (e.g., SqlGuidCaster from
202+
Microsoft.Data.SqlClient), causing GetTypes() to fail. When
203+
the exception occurs, use the Types collection from the
204+
exception which contains the types that were successfully
205+
loaded (with $null entries for failed types).
206+
#>
207+
foreach ($assembly in [System.AppDomain]::CurrentDomain.GetAssemblies())
208+
{
209+
try
210+
{
211+
$enumType = $assembly.GetTypes() |
212+
Where-Object -FilterScript { $_.FullName -eq $fullTypeName } |
213+
Select-Object -First 1
214+
215+
if ($enumType)
216+
{
217+
break
218+
}
219+
}
220+
catch [System.Reflection.ReflectionTypeLoadException]
221+
{
222+
$enumType = $_.Exception.Types |
223+
Where-Object -FilterScript { $null -ne $_ -and $_.FullName -eq $fullTypeName } |
224+
Select-Object -First 1
225+
226+
if ($enumType)
227+
{
228+
break
229+
}
230+
}
231+
}
201232
}
202233

203234
if ($enumType)

0 commit comments

Comments
 (0)