Skip to content

Commit 4429ee0

Browse files
committed
Refine enum type resolution in SqlResourceBase to filter SQL Server assemblies and handle ReflectionTypeLoadException more effectively
1 parent f6aa086 commit 4429ee0

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

source/Classes/011.SqlResourceBase.ps1

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -196,37 +196,28 @@ class SqlResourceBase : ResourceBase
196196
{
197197
<#
198198
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).
199+
Filter to only SQL Server assemblies to avoid loading types
200+
from unrelated assemblies like Microsoft.Data.SqlClient which
201+
can throw ReflectionTypeLoadException due to types that cannot
202+
be loaded (e.g., SqlGuidCaster). Also include assemblies that
203+
contain types in the Microsoft.SqlServer namespace (for stub
204+
types loaded via Add-Type in unit tests).
206205
#>
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-
}
206+
$sqlServerAssemblies = [System.AppDomain]::CurrentDomain.GetAssemblies() |
207+
Where-Object -FilterScript {
208+
$_.FullName -like 'Microsoft.SqlServer.*' -or
209+
$_.ExportedTypes.FullName -like 'Microsoft.SqlServer.*'
219210
}
220-
catch [System.Reflection.ReflectionTypeLoadException]
211+
212+
foreach ($assembly in $sqlServerAssemblies)
213+
{
214+
$enumType = $assembly.GetTypes() |
215+
Where-Object -FilterScript { $_.FullName -eq $fullTypeName } |
216+
Select-Object -First 1
217+
218+
if ($enumType)
221219
{
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-
}
220+
break
230221
}
231222
}
232223
}

0 commit comments

Comments
 (0)