在尝试在面向.NET Core的类库包中使用Json.NET的 JsonConvert.DeserializeObject 方法时,我无法解析缺少的引用。
在VS2015中重现:
在创建的默认Class1中,添加以下简单方法:
public void DoSomething()
{
var x = JsonConvert.DeserializeObject("");
}
您将收到以下错误:
“Object”类型在未引用的程序集中定义。您必须添加对程序集“mscorlib,Version = 2.0.5.0”
的引用
知道我缺少什么参考吗?我假设它是某种System.Serialization或System.IO包,但我无法弄清楚哪一个。
project.json:
{
"version": "1.0.0-*",
"description": "ClassLibrary1 Class Library",
"authors": [ "DD" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"frameworks": {
"net451": { },
"netcore50": {
"dependencies": {
"Microsoft.CSharp": "4.0.0",
"System.Collections": "4.0.0",
"System.Linq": "4.0.0",
"System.Runtime": "4.0.0",
"System.Threading": "4.0.0"
}
}
},
"dependencies": {
"Newtonsoft.Json": "8.0.2"
}
}
答案 0 :(得分:3)
您可以通过添加Microsoft.NETCore.Portable.Compatibility
的依赖项来解决此问题。 [NuGet page]
{
"version": "1.0.0-*",
"description": "ClassLibrary1 Class Library",
"authors": [ "DD" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"frameworks": {
"net451": { },
"netcore50": {
"dependencies": {
"Microsoft.CSharp": "4.0.0",
"System.Collections": "4.0.0",
"System.Linq": "4.0.0",
"System.Runtime": "4.0.0",
"System.Threading": "4.0.0"
}
}
},
"dependencies": {
"Newtonsoft.Json": "8.0.2",
"Microsoft.NETCore.Portable.Compatibility": "1.0.1-beta-23516"
}
}
答案 1 :(得分:1)
您可以使用最新的Newtonsoft.Json nuget程序包(目前为11.0.2),而.NET Core 2.1则没有任何问题。