简体   繁体   English

无法调用/加载.NET Core程序集,但.NET Standard在PowerShell中使用Add-Type可以正常工作

[英]Can't call/load .NET Core assembly but .NET Standard works fine using Add-Type in PowerShell

While attempting to create a .NET Core class library for PowerShell I'm seeing some strange differences between .NET Standard and .NET Core. 在尝试为PowerShell创建.NET Core类库时,我发现.NET Standard和.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] . 按照指南,我创建了一个简单的.NET Standard 2.0类库,并且能够使用Add-Type -Path [path]在PowerShell 5.1和PowerShell 6.0.1中成功加载它。

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. 我可以调用.Bark()方法并在PowerShell 5.1和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. 当我使用.NET Core 2.2模板构建同一个类时,我无法在PowerShell实例中使用它。 Code is almost identical: 代码几乎相同:

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

PowerShell 5.1: PowerShell 5.1:

Add-Type : Unable to load one or more of the requested types. Add-Type:无法加载一个或多个请求的类型。 Retrieve the LoaderExceptions property for more information. 检索LoaderExceptions属性以获取更多信息。

PowerShell Core 6.0.1 PowerShell Core 6.0.1

Unable to find type [AnimalCore.Dog] 无法找到类型[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? 我很高兴继续使用.NET标准模板来构建我的程序集但是我想知道这里有什么区别以及如何编译这个类库以便在.NET Core上使用?

In reverse order: 按相反顺序:

how it would be possible to compile this class library for use on .NET Core? 如何编译这个类库以便在.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). 为了让您的库被PowerShell Core使用,您现在必须以.NET Core 2.1为目标 - 2.2尚不支持(尽管我的理解是下一个版本PowerShell Core 6.2将会看到项目已迁移到.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 我很高兴继续使用.NET标准模板来构建我的程序集,但我想知道这里的区别是什么

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. 您可能知道,.NET Framework和.NET Core之间没有完全的功能奇偶校验 - 自然提供的一些基类库在.NET Core中不可用,并且.NET中需要一些跨平台支持。某些API的核心结果与.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! 这种可移植性差距的解决方案称为.NET标准 - 它定义了在Framework和Core之间完全重叠的大型API子集!

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 因此,当您使用netstandard2.0而不是netcoreapp2.2netcoreapp2.1 ,它可以确保.NET Framework .NET Core运行时都可以引用和使用您的库 - 这正是您发现代码完美运行的原因只要您定位.NET Standard,就可以在PowerShell 5.1和6.0.1中使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Add-Type PowerShell 2.0找不到方法 - Add-Type PowerShell 2.0 can't find method 无法将自定义 ActiveX 控件添加到 .NET Core WinForms 应用程序,但 .NET Framework 应用程序工作正常 - Can't add custom ActiveX control to .NET Core WinForms app., but .NET Framework app works fine PowerShell Add-Type Simple C# 结构——一种有效,一种无效 - PowerShell Add-Type Simple C# Structures -- One works and one doessn't 如何通过命令行在 powershell 中使用 Add-Type 以执行 VB.NET 或 C#(或 JScript.net)代码? - How I can use Add-Type in powershell through command line in order to execute VB.NET or C# (or JScript.net) code? 引用.NET Standard项目时,NET Framework项目无法加载程序集 - NET Framework project can't load assembly when referencing .NET Standard project 无法在Powershell中加载.NET程序集 - Unable to load .NET assembly in Powershell 程序集.加载 .NET Core - Assembly.Load .NET Core 无法从.Net Core 3.0 中的程序集加载类型 - Could not load type from assembly in .Net Core 3.0 无法发布.NET Core Web Application(无法加载文件或程序集Microsoft.DotNet.ProjectModel) - Can't publish .NET Core Web Application (could not load file or assembly Microsoft.DotNet.ProjectModel) 无法调试 .NET Core:无法加载文件或程序集“System.Runtime,版本 = 4.2.1.0 / 4.2.0.0 - Can't debug .NET Core: Could not load file or assembly 'System.Runtime, Version=4.2.1.0 / 4.2.0.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM