[英]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!";
}
}
}
Add-Type : Unable to load one or more of the requested types. Add-Type:无法加载一个或多个请求的类型。 Retrieve the LoaderExceptions property for more information. 检索LoaderExceptions属性以获取更多信息。
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.2
或netcoreapp2.1
,它可以确保.NET Framework 和 .NET Core运行时都可以引用和使用您的库 - 这正是您发现代码完美运行的原因只要您定位.NET Standard,就可以在PowerShell 5.1和6.0.1中使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.