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

3

我在谷歌上花了很长时间,所以如果这是一个简单的修复,请原谅我,但我在C#中找不到任何与此特定问题相关的解决方案。

在尝试使用以下代码时,它会给我一个错误:

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'.

我搜索到的唯一问题是人们遇到版本不匹配的错误,但据我所知这并不是我的问题。

完整代码如下:(“logHigh”是我使用的日志记录函数)

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);

类定义:

[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; }
}

使用Visual Studio 2019

通过NuGet包管理器添加了Newtonsoft

  • 版本为12.0.3

对象声明是临时的。稍后将切换到从网页加载JSON,但我只是试图在第一次实例中使其正常工作,因此使用了这个函数名称。

可能有一个简单的解决方法,但不幸的是,我迄今为止无法找到它。

基于评论和测试的额外信息:

  • 这是一个游戏mod的类库,而不是Web项目。
  • 添加:
<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>

无论是否存在,app.config都不起作用。

  • 在VS 2019中,Newtonsoft.Json的引用没有任何警告。
  • 重建项目没有效果。(必须这样做,因为一开始当我添加任何引用该DLL的代码后,我的函数就拒绝运行了。成功重建后,我最终遇到了上述错误。)
  • 位于 bin\Debug 中的DLL版本与在VS 2019中引用的版本相同。
  • 创建一个全新的项目并复制代码也不起作用。
  • 更改目标框架(使用相应的DLL版本4.5或4.7.2)也没有效果。
  • 尝试以下Newtonsoft.Json的先前版本,但都无法成功:
    • 12.0.3
    • 11.0.2
    • 10.0.3
    • 9.0.1
    • 8.0.3

@RuiCaramalho 不是的,它是一个游戏模组的类库。 - cheesenibbles
另一个可能的解决方案是删除 binobj 文件夹并重新构建项目。确保根据您所在的 DebugRelease 环境删除正确的文件夹。 - Rui Caramalho
我已经尝试了重建,但没有效果。不幸的是,我必须这样做,因为最初我的函数在引用NewtonSoft时根本无法运行,通过这样做,我终于让try{}catch(){}抛出了上面的错误。 我会再试一次,但我怀疑它并不会有太大作用。 - cheesenibbles
Alternion.csproj 包含一个链接 <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath> </Reference> - cheesenibbles
就好像说引用了12.0.0.0的项目应该接受12.0.3版本。 - Rui Caramalho
显示剩余12条评论
2个回答

0

我曾经遇到过同样的问题,似乎只有在newversion="12.0.0.0"的情况下才能正常工作,即使你使用的是Newtonsoft.Json的版本12.0.3.23909。

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
    <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" /> 
</dependentAssembly>

0

这是我需要做的事情...

我将 Newtonsoft.json 添加到项目中,然后在 TargetFramework 下面添加了以下指令:

在 dotnet core 中,<dependentAssembly> 似乎被忽略了,而下面的指令才能使其正常工作...

希望对您有用

关于 AutoGenerateBindingRedirects 的链接 https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/how-to-enable-and-disable-automatic-binding-redirection

以及 GenerateBindingRedirectsOutputType 的链接 https://github.com/dotnet/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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接