【发布时间】:2020-11-09 15:08:14
【问题描述】:
我使用 Add-Type 加载了一个程序集:
$Typename = '\\crtwfaadvlkv0.d2dbfg.com\PRODUCTION\Vision\Apps\VisionPipeline\Oracle.ManagedDataAccess.dll'
Add-Type -LiteralPath $TypeName
并确认已加载
> [appdomain]::CurrentDomain.GetAssemblies() |
>> Sort-Object -Property FullName |
>> Select-Object -Property FullName;
(partial results)
Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342
接下来我想加载程序集中定义的类,以便我可以使用它们,但出现以下错误:
> $oracletpe = Add-Type -AssemblyName 'Oracle.ManagedDataAccess' -PassThru
Add-Type : Cannot add type. The assembly 'Oracle.ManagedDataAccess' could not be found.
At line:1 char:14
+ ... oracletpe = Add-Type -AssemblyName 'Oracle.ManagedDataAccess' -PassTh ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Oracle.ManagedDataAccess:String) [Add-Type], Exception
+ FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : Cannot add type. One or more required assemblies are missing.
At line:1 char:14
+ ... oracletpe = Add-Type -AssemblyName 'Oracle.ManagedDataAccess' -PassTh ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : ASSEMBLY_LOAD_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand
因此,PowerShell 似乎找不到它刚刚加载的程序集。我做错了什么?
【问题讨论】:
-
为什么要加载程序集两次(第一次按路径,第二次按名称)?只需将“-PassThru”参数添加到第一个 Add-Type 命令(使用 LiteralPath)并将结果分配给变量。
-
我不想加载它两次。我想我不明白这是如何运作的。我正在尝试加载 dll,然后在第二次调用中,将类型加载到命名空间中。我很困惑 Posh 找不到程序集,因为它刚刚加载了它。
标签: types .net-assembly powershell-5.0