【发布时间】:2019-02-22 21:40:21
【问题描述】:
在尝试为 PowerShell 创建 .NET Core 类库时,我发现 .NET Standard 和 .NET Core 之间存在一些奇怪的差异。
按照this 的指导,我创建了一个简单的 .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!";
}
}
}
我可以调用 .Bark() 方法并在 PowerShell 5.1 和 PowerShell Core 6.0.1 中查看输出。
当我使用 .NET Core 2.2 模板构建相同的类时,我无法在任一 PowerShell 实例中使用它。代码几乎相同:
namespace AnimalCore
{
public class Dog
{
public string Bark()
{
return "Woof!";
}
}
}
PowerShell 5.1:
Add-Type : 无法加载一种或多种请求的类型。检索 LoaderExceptions 属性以获取更多信息。
PowerShell 核心 6.0.1
找不到类型 [AnimalCore.Dog]
我可以愉快地继续使用 .NET Standard 模板来构建我的程序集,但我想知道这里有什么区别以及如何编译这个类库以在 .NET Core 上使用?
【问题讨论】:
-
LoaderExceptions说明了什么?您应该可以使用$Error[0].Exception.LoaderExceptions进行检查
标签: c# powershell