Add-Type -TypeDefinition 'namespace MyNamespace { public class c {}}'
$assembly =
$([System.AppDomain]::CurrentDomain.
GetAssemblies().
GetTypes() |
? {$_.Namespace -eq 'MyNamespace' } |
% Assembly |
Select-Object -Unique -First 1 )
Add-Type -TypeDefinition 'namespace MyNamespace {public class d : c {}}' `
-ReferencedAssemblies $assembly
Попытка завершается ошибкой
Add-Type: C:\repro.ps1:12
Line |
12 | Add-Type -TypeDefinition 'namespace MyNamespace {public class d : c { …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| The value cannot be an empty string. (Parameter 'path')
что предполагает, что Add-Type ищет путь, который, вероятно, не существует, поскольку сборка существует только в памяти.
Можно ли добавить тип, который ссылается на тип, хранящийся только в памяти?
Что работает
Следующий подход работает, но требует существования сборок в файловой системе:Remove-Item .\c.dll,.\d.dll -ErrorAction SilentlyContinue
Add-Type `
-TypeDefinition 'namespace MyNamespace { public class c {}}' `
-OutputAssembly .\c.dll
Add-Type -Path .\c.dll
[MyNamespace.c]::new()
Add-Type -TypeDefinition 'namespace MyNamespace {public class d : c {}}' `
-ReferencedAssemblies .\c.dll `
-OutputAssembly .\d.dll
Add-Type -Path .\d.dll
[MyNamespace.d]::new()
Подробнее здесь: https://stackoverflow.com/questions/791 ... y-assembly