简体   繁体   English

无法从程序集 Newtonsoft.Json 加载类型“Newtonsoft.Json.JsonConvert”

[英]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 完成

  • Version 12.0.3版本 12.0.3

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.可能有一个简单的修复方法,但不幸的是,到目前为止我还没有找到它。

Extra information based on comments and testing:基于评论和测试的额外信息:

  • It's a class library for a game mod, not a web project.这是一个游戏模组的 class 库,而不是 web 项目。
  • Adding:添加:
<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都没有影响。

  • The Newtonsoft.Json reference within VS 2019 has no warning(s) VS 2019 中的 Newtonsoft.Json 参考没有警告
  • Rebuilding the project has no effect, (had to do this initially as my function refused to run originally as soon as I added any code referencing the DLL, after a successful rebuild I was finally presented with the error above)重建项目没有效果,(最初不得不这样做,因为我的 function 在我添加任何引用 DLL 的代码后拒绝运行,在成功重建后我终于看到了上面的错误)
  • The dll version that is located in bin\Debug is the same version as referenced within VS 2019位于bin\Debug中的 dll 版本与 VS 2019 中引用的版本相同
  • Creating a fresh project and copying the code over, did not work创建一个新项目并将代码复制过来,没有用
  • Changing target framework (either 4.5 or 4.7.2 (and using respective dll version(s)) had no effect更改目标框架(4.5 或 4.7.2(并使用相应的 dll 版本)无效)
  • The following previous versions of Newtonsoft.Json were tried, to no success:尝试了以下以前版本的 Newtonsoft.Json,但没有成功:
    • 12.0.3 12.0.3
    • 11.0.2 11.0.2
    • 10.0.3 10.0.3
    • 9.0.1 9.0.1
    • 8.0.3 8.0.3

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#L2027GenerateBindingRedirectsOutputType 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.

相关问题 无法加载文件或程序集“ Newtonsoft.Json” - Could not load file or assembly 'Newtonsoft.Json' 无法加载文件或程序集“newtonsoft.json” - could not load file or assembly 'newtonsoft.json' 'Newtonsoft.Json 中都存在'JsonConvert' 类型 - The type 'JsonConvert' exists in both 'Newtonsoft.Json Newtonsoft.Json Json转换为数据表 - Newtonsoft.Json JsonConvert To Datatable 由 Newtonsoft.Json.JsonConvert 从同一个类序列化 nad 反序列化 - Serialize nad Deserialize from this same Class by Newtonsoft.Json.JsonConvert &#39;Newtonsoft.Json.dll&#39;和&#39;NuGetApi2.dll&#39;中都存在&#39;Newtonsoft.Json.JsonConvert&#39;类型 - The type 'Newtonsoft.Json.JsonConvert' exists in both 'Newtonsoft.Json.dll' and 'NuGetApi2.dll' Newtonsoft.Json版本8.0.2无法加载文件或程序集错误 - Newtonsoft.Json version 8.0.2 Could not load file or assembly Error 无法加载文件或程序集 &#39;Newtonsoft.Json,版本 = 12.0.0.0? - Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0? 无法加载文件或程序集 &#39;Newtonsoft.Json,版本 = 10.0.0.0 - Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0 无法加载文件或程序集 &#39;Newtonsoft.Json,版本 = 7.0.0.0 - Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM