[英]Could not load type 'Newtonsoft.Json.JsonConvert' from assembly Newtonsoft.Json
I spent a good while on google, so if this is a simple fix forgive me but I cannot find anything relating to a fix to this specific issue within C#.我在谷歌上花了很长时间,所以如果这是一个简单的修复请原谅我,但我在 C# 中找不到与此特定问题的修复相关的任何内容。
When trying to use the following code it gives me this error:当尝试使用以下代码时,它给了我这个错误:
string obj = JsonConvert.SerializeObject(playerObject);
Could not load type 'Newtonsoft.Json.JsonConvert' from assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.
The only thing I can really find when searching is people running into version mis-match errors, which is not my issue here as far as I can tell.在搜索时我唯一能真正找到的是人们遇到版本不匹配错误,据我所知这不是我的问题。
Full code for reference: ("logHigh" is a logging function I use)完整代码供参考:(“logHigh”是我用的一个logging function)
public void convertToObject(webPlayerObjArray playerObject)
{
logHigh("Entered function");
try
{
logHigh("Serializing");
string obj = JsonConvert.SerializeObject(playerObject);
logHigh(obj);
webPlayerObjArray plrobjarr = JsonConvert.DeserializeObject<webPlayerObjArray>(obj);
logHigh(plrobjarr.users[0].badgeName);
}catch (Exception e)
{
logHigh(e.Message);
}
logHigh("Finished function");
}
webPlayerObjArray playerFile = new webPlayerObjArray();
webObject webObj1 = new webObject();
webObj1.steamID = "test1";
webObj1.badgeName = "test2";
webObj1.weaponSkinName = "test3";
webObj1.sailName = "test4";
webObj1.cannonSkinName = "test5";
webObject webObj2 = new webObject();
webObj2.steamID = "test1";
webObj2.badgeName = "test2";
webObj2.weaponSkinName = "test3";
webObj2.sailName = "test4";
webObj2.cannonSkinName = "test5";
playerFile.users = new webObject[2];
playerFile.users[0] = webObj1;
playerFile.users[1] = webObj2;
logHigh(playerFile.users[0].badgeName);
logHigh(playerFile.users[1].badgeName);
Class definitions: Class定义:
[System.Serializable]
public class webObject
{
public string steamID { get; set; }
public string badgeName { get; set; }
public string sailName { get; set; }
public string weaponSkinName { get; set; }
public string cannonSkinName { get; set; }
}
[System.Serializable]
public class webPlayerObjArray
{
public webObject[] users { get; set; }
}
Using visual studio 2019 Addition of Newtonsoft done through NuGet package Manager使用 visual studio 2019 添加 Newtonsoft 通过 NuGet package Manager 完成
Object declarations are temporary. Object 声明是临时的。 Going to switch to loading json from a webpage later on, but i was just trying to get it to work in the first instance, hence the function name.稍后将切换到从网页加载 json,但我只是试图让它在第一个实例中工作,因此名称为 function。
There's probably an easy fix, but I have unfortunately been unable to find it thus far.可能有一个简单的修复方法,但不幸的是,到目前为止我还没有找到它。
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.3" /> //12.0.3.23909 also had no effect
</dependentAssembly>
To the app.config
unfortunately had no effect, whether present or not.不幸的是,无论是否存在,对app.config
都没有影响。
bin\Debug
is the same version as referenced within VS 2019位于bin\Debug
中的 dll 版本与 VS 2019 中引用的版本相同I had the same problem, it seams it works only with newversion="12.0.0.0" even if you use Version 12.0.3.23909 of Newtonsoft.Json.我遇到了同样的问题,即使您使用 Newtonsoft.Json 的版本 12.0.3.23909,它也只能与 newversion="12.0.0.0" 一起使用。
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
</dependentAssembly>
This what I need to do...这是我需要做的...
I added Newtonsoft.json
to the project and then those directives below the TargetFramework我将Newtonsoft.json
添加到项目中,然后将这些指令添加到 TargetFramework 下面
In do.net core the <dependentAssembly>
it seems is ignored and the those below are the ones that would make it work....在 do.net 核心中, <dependentAssembly>
似乎被忽略了,下面的那些是可以让它工作的....
Hope it works for you希望这对你有用
The link https://learn.microsoft.com/en-us/do.net/framework/configure-apps/how-to-enable-and-disable-automatic-binding-redirection about AutoGenerateBindingRedirects
关于AutoGenerateBindingRedirects
的链接https://learn.microsoft.com/en-us/do.net/framework/configure-apps/how-to-enable-and-disable-automatic-binding-redirection
And the GenerateBindingRedirectsOutputType
https://github.com/do.net/msbuild/blob/dd5e8bc3f86ac98bd77d8971b00a6ad14f122f1a/src/XMakeTasks/Microsoft.Common.CurrentVersion.targets#L2027和GenerateBindingRedirectsOutputType
https://github.com/do.net/msbuild/blob/dd5e8bc3f86ac98bd77d8971b00a6ad14f122f1a/src/XMakeTasks/Microsoft.Common.CurrentVersion.targets#L2027
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Flurl.Http" Version="3.0.1" />
<PackageReference Include="Newtonsoft.json" Version="13.0.1" />
</ItemGroup>
</Project>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.