无法调用/加载 .NET Core 程序集,但 .NET Standard 在 PowerShell 中使用 Add-Type 工作正常 [英] Can't call/load .NET Core assembly but .NET Standard works fine using Add-Type in PowerShell

查看:60
本文介绍了无法调用/加载 .NET Core 程序集,但 .NET Standard 在 PowerShell 中使用 Add-Type 工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试为 PowerShell 创建 .NET Core 类库时,我发现 .NET Standard 和 .NET Core 之间存在一些奇怪的差异.

遵循在这个指南中,我创建了一个简单的 .NET Standard 2.0 类库,并且能够使用 Add-Type -Path [path].

命名空间 Animal{公开课狗{公共字符串 Bark(){返回 "Woof!";}}}

我可以调用 .Bark() 方法并在 PowerShell 5.1 和 PowerShell Core 6.0.1 中查看输出.

当我使用 .NET Core 2.2 模板构建相同的类时,我无法在任一 PowerShell 实例中使用它.代码几乎相同:

命名空间 AnimalCore{公开课狗{公共字符串 Bark(){返回 "Woof!";}}}

PowerShell 5.1:

<块引用>

Add-Type:无法加载一种或多种请求的类型.检索 LoaderExceptions 属性以获取更多信息.

PowerShell 核心 6.0.1

<块引用>

无法找到类型 [AnimalCore.Dog]

我很乐意继续使用 .NET Standard 模板来构建我的程序集,但我想知道这里有什么区别以及如何编译此类库以在 .NET Core 上使用?

解决方案

倒序:

<块引用>

如何编译此类库以在 .NET Core 上使用?

为了让 PowerShell Core 使用您的库,您现在必须以 .NET Core 版本 2.1 为目标 - 2.2 尚不受支持(尽管我的理解是下一个版本,PowerShell Core 6.2,将看到项目迁移到 .NET Core 2.2).

<小时><块引用>

我很乐意继续使用 .NET Standard 模板来构建我的程序集,但我想知道这里有什么区别

如您所知,.NET Framework 和 .NET Core 之间的功能并不完全相同——Windows 附带的一些基类库自然在 .NET Core 中不可用,并且需要一些跨平台支持在 .NET Core 中导致某些 API 的行为与在 .NET Framework 中不同.

解决这种可移植性差距的方法称为 .NET Standard - 它定义了框架和核心之间完美重叠的大量 API 子集!

因此,当您针对 netstandard2.0 而不是 netcoreapp2.2netcoreapp2.1 时,这是一种确保 .NETFramework .NET Core 运行时可以引用和使用您的库 - 这正是您发现代码在 PowerShell 5.1 和 6.0.1 中都能完美运行的原因,只要您以 .NET Standard 为目标

While attempting to create a .NET Core class library for PowerShell I'm seeing some strange differences between .NET Standard and .NET Core.

Following this guidance, I've created a simple .NET Standard 2.0 class library and have been able to successfully load it in PowerShell 5.1 and PowerShell 6.0.1 using Add-Type -Path [path].

namespace Animal
{
    public class Dog
    {
        public string Bark()
        {
            return "Woof!";
        }
    }
}

I can call the .Bark() method and see an output in PowerShell 5.1 and PowerShell Core 6.0.1.

When I use the .NET Core 2.2 template to build the same class I can't use it in either PowerShell instance. Code is almost identical:

namespace AnimalCore
{
    public class Dog
    {
        public string Bark()
        {
            return "Woof!";
        }
    }
}

PowerShell 5.1:

Add-Type : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

PowerShell Core 6.0.1

Unable to find type [AnimalCore.Dog]

I could happily go on using the .NET Standard template to build my assembly but I'm wondering what the difference is here and how it would be possible to compile this class library for use on .NET Core?

解决方案

In reverse order:

how it would be possible to compile this class library for use on .NET Core?

In order for your library to be used by PowerShell Core, you'll have to target .NET Core version 2.1 for now - 2.2 is not yet supported (although it's my understanding that the next release, PowerShell Core 6.2, will see the project migrated to .NET Core 2.2).


I could happily go on using the .NET Standard template to build my assembly but I'm wondering what the difference is here

As you may know, there's not complete feature parity between the .NET Framework and .NET Core - some base class libraries that ship with Windows naturally aren't available in .NET Core, and some of the cross-platform support required in .NET Core results in some APIs behaving differently than in .NET Framework.

The solution to this portability gap is called .NET Standard - it defines the large subset of APIs that perfectly overlap between Framework and Core!

So when you target netstandard2.0 rather than netcoreapp2.2 or netcoreapp2.1, it's a way to ensure that both .NET Framework and .NET Core runtimes can reference and use your library - which is exactly why you find that your code works perfectly in both PowerShell 5.1 and 6.0.1 as long as you target .NET Standard

这篇关于无法调用/加载 .NET Core 程序集,但 .NET Standard 在 PowerShell 中使用 Add-Type 工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆