【问题标题】:Loading .NetCore DLL's in powershell在 powershell 中加载 .Net Core DLL
【发布时间】:2020-05-18 13:47:37
【问题描述】:

我创建了一些使用 .NetCore 1.1 编译的 .DLL。我现在需要将它们加载到 Powershell 并调用它们的方法。这是支持还是我的 DLL 需要使用完整版本的 .Net 编译?

【问题讨论】:

  • PowerShell 基于 .NET Framework,而不是 .NET Core。如果您针对适当的netstandard profile 构建程序集(并且还部署了它的依赖项),它应该在 .NET Framework 上运行。如果您仅将其显式构建为 .NET Core,则可能不会。

标签: .net powershell .net-core


【解决方案1】:

您的 .NET Core dll 不会在标准开箱即用的 Windows 仅风味的 powershell 上运行。

但是!这里有一个 .NET Core 友好版本的 powershell:Powershell Core

Powershell Core 将在 Windows、Linux 和 macOS 上运行,就像 .NET Core 的本意一样。

【讨论】:

    【解决方案2】:

    我在尝试做类似的事情时遇到了这个问题。我通过以下方式成功加载了 .NET Core dll:

    // You can use namespaces, but also paths
    using assembly 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.4\System.Text.Json.dll'
    
    // Specific for the static method I use in my example
    $options = New-Object System.Text.Json.JsonSerializerOptions
    
    // Use the Serialize method of the static class JsonSerializer
    $jsonString = [System.Text.Json.JsonSerializer]::Serialize($device, $options);
    

    我使用了 System.Text.Json,但您可以使用任何 dll 执行此操作。

    来源: Powershell 7 docs

    【讨论】:

      【解决方案3】:

      要将 DLL 添加到 powershell 脚本,请使用以下代码:Add-Type -Path "C:\locationOfDLL\mydll.dll"。看看这是否适用于根据需要访问 dll。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-21
        • 1970-01-01
        • 2020-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-30
        • 1970-01-01
        相关资源
        最近更新 更多