Powershell: Add-Type -Assembly -PassThru sometimes doesn't list types

Created on 16 Oct 2019  路  4Comments  路  Source: PowerShell/PowerShell

Add-Type -Assembly -PassThru is expected to list all types loaded from the assembly (even if the assembly has already been loaded).

Currently, this fails quietly with some assemblies.

This is a regression from Windows PowerShell.

Steps to reproduce

On macOS or Windows (haven't tried on Linux):

Add-Type -AssemblyName System.Numerics -PassThru | Should -Not -BeNullOrEmpty

Note that when you inspect one of the types supposedly from this assembly with .AssemblyName - e.g., [bigint].AssemblyName - it points to a _different_ assembly, System.Runtime.Numerics.dll, and using the above command with _that_ works, so the problem perhaps relates to "stub"(?) assemblies such as System.Numerics.dll?

_Update_: See @0xd4d's comment below

Expected behavior

The test should succeed.

Actual behavior

The test fails, because the command produces no output

Environment data

PowerShell Core 7.0.0-preview.4 on macOS 10.15
PowerShell Core 7.0.0-preview.4 on Microsoft Windows 10.0.17763
Issue-Question

Most helpful comment

System.Numerics (on .NET Core at least) is an assembly that only has types forwarded to other assemblies.

The types are forwarded to System.Numerics.Vectors and System.Runtime.Numerics, although that is most likely an implementation detail you shouldn't depend on.

You can get the forwarded types by calling GetForwardedTypes() instead of GetTypes().

$asm = [reflection.assembly]::Load('System.Numerics')
# empty
$asm.GetTypes()
# returns all types
$asm.GetForwardedTypes()
# Show assemblies
$asm.GetForwardedTypes() | ForEach-Object { $_.Assembly.Location }

All 4 comments

c# private void WriteTypes(Assembly assembly) { WriteObject(assembly.GetTypes(), true); }
In the code we have the assembly and assembly.GetTypes() returns {System.RuntimeType[0]} (debugger view) that is empty.

I think it is the same as our Microsoft.PowerShell.SDK.dll #3401

System.Numerics (on .NET Core at least) is an assembly that only has types forwarded to other assemblies.

The types are forwarded to System.Numerics.Vectors and System.Runtime.Numerics, although that is most likely an implementation detail you shouldn't depend on.

You can get the forwarded types by calling GetForwardedTypes() instead of GetTypes().

$asm = [reflection.assembly]::Load('System.Numerics')
# empty
$asm.GetTypes()
# returns all types
$asm.GetForwardedTypes()
# Show assemblies
$asm.GetForwardedTypes() | ForEach-Object { $_.Assembly.Location }

/cc @SteveL-MSFT @daxian-dbw Should we passthru forwarded types too?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SteveL-MSFT picture SteveL-MSFT  路  3Comments

alx9r picture alx9r  路  3Comments

JohnLBevan picture JohnLBevan  路  3Comments

andschwa picture andschwa  路  3Comments

abock picture abock  路  3Comments