【问题标题】:Import-Module : Could not load file or assembly in Powershell导入模块:无法在 Powershell 中加载文件或程序集
【发布时间】:2023-04-07 09:04:02
【问题描述】:

我正在尝试将一些 .dll 文件从我的应用程序导入我的 powershell 脚本。 有两个 dll:MyProject.dllMyProject.Data.dll。我可以成功加载第一个 dll,我可以访问那里的 ENUMS。但问题是,当我尝试加载第二个时,它会抛出此错误:

Import-Module:无法加载文件或程序集“System.Runtime,Version=4.2.1.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”或其依赖项之一。系统找不到指定的文件。

这是我的脚本的样子:

Import-Module "./dll/MyProject.dll"
Import-Module "./dll/MyProject.Data.dll" # error while loading this one.


$global:admin_role = [MyProject.UserRole]::Admin
$global:super_user_role = [MyProject.UserRole]::SuperUser


$user_account = New-Object MyProject.Data.UserAccount

仅供参考,我从 MyProject.API/bin/Debug/netcoreapp2.2 获得了包含所有 dll 的 dll。

【问题讨论】:

    标签: c# powershell dll .net-core


    【解决方案1】:

    尝试使用 Add-Type 加载您的 dll:

    Add-Type -Path .\dll\MyProject.dll
    Add-Type -Path .\dll\MyProject.Data.dll
    

    【讨论】:

    • 没有参数Type。这是 Add-Type 包含的内容:Add-Type [-TypeDefinition] <string> [-CodeDomProvider <CodeDomProvider>] [-CompilerParameters <CompilerParameters>] [-Language <Language>] [-ReferencedAssemblies <string[]>] [-OutputAssembly <string>] [-OutputType <OutputAssemblyType>] [-PassThru] [-IgnoreWarnings] [<CommonParameters>]
    猜你喜欢
    • 1970-01-01
    • 2019-06-10
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-31
    • 2020-08-17
    相关资源
    最近更新 更多