I'm writing a PowerShell script that uses the reflection APIs to get all the namespaces in an assembly. Anyways, that's not relevant. Here is the relevant portion of the code:
function Get-Namespaces($assembly)
{
$assemblyClass = [Reflection.Assembly]
$assemblyObject = $assemblyClass::ReflectionOnlyLoadFrom($assembly)
$types = $assemblyObject.GetTypes() # This is the part that's having issues
return $types | ? IsPublic | select Namespace -Unique
}
cd $PSScriptRoot
$assemblies = @()
$assemblies += Get-WpfAssemblies
$assemblies += Get-UwpAssembly
$namespaces = $assemblies | % {
% { Get-Namespaces $_ }
}
For some reason, the part that initializes $types
seems to be having issues; specifically, it's telling me to catch the exception and check the LoaderExceptions
property of the caught exception for more information. So when I try to do just that:
try { $assemblyObject.GetTypes() } catch { echo $_.LoaderExceptions }
and run it, the script prints nothing.
Why does this happen and how can I fix it?
For people who would like to try out the script in its entirety, I've made a publicly available GitHub gist. (Note that it will only work if you have the Windows 10 dev tools installed, but I'm sure reasonably experienced PowerShell users can modify the script to run on their machines.)