【发布时间】:2020-01-04 23:19:52
【问题描述】:
- 我在 VS Code 中创建了一个新的类库项目。
- 我在我的项目中添加了两个包:
PowerShellStandard.Library+System.Text.Json。
我的csproj 文件包含此块:
<ItemGroup>
<PackageReference Include="PowerShellStandard.Library" Version="5.1.1" />
<PackageReference Include="System.Text.Json" Version="4.7.0" />
</ItemGroup>
我的.cs 文件使用System.Text.Json 和System.Management.Automation。
当我使用JsonSerializer.Serialize(...) 时,它不会在 VS Code 中向我抛出任何错误/警告。在运行dotnet build 时,它也可以无错误或警告地编译。我可以导入它,但最后,当我运行代码时,我收到以下错误:
Get-JsonString : Could not load file or assembly 'System.Text.Json, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
At line:1 char:1
+ Get-JsonString -input s
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-JsonString], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,UrlCSharpPowerShell.CreateJson
我在这里错过了什么?
【问题讨论】:
-
还有哪些依赖?看起来其他东西正在引用
System.Text.Json库,因为错误消息中的版本与PackageReference版本不匹配 -
您的目标是哪个 .NET 版本?
-
@SimplyGed,我尝试添加
System.Text.Json --version 4.0.1,但它添加了 4.6 版。之后,我收到了同样的错误信息。我尝试使用NewtonSoft.Json。即使我将此包添加到我的 csproject 文件中,VS Code 也会引发警告,即当前上下文中不存在 NewtonSoft 或找不到命名空间。 @PavelAnikhouski,我想用 C# 编译 PowerShell 函数并想为 .NET Core 编写它。 -
一个解决方法是使用Newtonsoft.Json。
-
您解决了这个问题还是找到了根本原因?