简体   繁体   English

如何更改 netstandard 2.0 库使用的 Newtonsoft.Json 的版本

[英]How to change the version of Newtonsoft.Json used by netstandard 2.0 library

I created the netstadard 2.0 class library that references Newtonsoft.Json 12.0.3 nuget.我创建了引用 Newtonsoft.Json 12.0.3 nuget 的 netstadard 2.0 class 库。 When I build the library and check the referenced assemblies via Assembly.GetReferencedAssemblies() I see that my library references Newtonsoft.Json version 12.0.0 instead of 12.0.3.当我构建库并通过Assembly.GetReferencedAssemblies()检查引用的程序集时,我看到我的库引用了 Newtonsoft.Json 版本 12.0.0 而不是 12.0.3。

Publishing the same library via dotnet publish command copies 12.0.3 version of Newtonsoft.Json as well.通过dotnet publish命令发布相同的库也会复制 12.0.3 版本的 Newtonsoft.Json。

However, if I try to load that netstandard library from .net core 3.1 app dynamically using Assembly.Load() and then use reflection to access type information I get the exception that Newtonsoft.Json version 12.0.0 could not be loaded.但是,如果我尝试使用Assembly.Load()动态地从 .net 核心 3.1 应用程序加载该 netstandard 库,然后使用反射来访问类型信息,则会出现无法加载 Newtonsoft.Json 版本 12.0.0 的异常。

How can I force my class library to reference the 12.0.3 version instead of 12.0.0 which does not even exist on nuget?如何强制我的 class 库引用 12.0.3 版本而不是 nuget 上甚至不存在的 12.0.0?

The Assembly version in Newtonsoft.Json returns 12.0.0.0 by design. Newtonsoft.Json中的程序集版本按设计返回 12.0.0.0。 Package Versions and Assembly Versions aren't directly tied together and for various reasons can be different. Package 版本和组装版本没有直接联系在一起,并且由于各种原因可能会有所不同。 (I for one sometimes forget to update the assembly version when I increment my package version.) (当我增加 package 版本时,我有时会忘记更新程序集版本。)

If you look at the buildScripts.ps1 in the repo you can see where AssemblyVersion is specifically set to the major version plus 0.0 when the AssemblyVersion is empty - which it is, since it isn't set anywhere else in the script.如果您查看存储库中的buildScripts.ps1 ,您可以看到在 AssemblyVersion 为空时,AssemblyVersion 被专门设置为主要版本加上0.0 - 确实如此,因为它没有在脚本中的其他任何地方设置。

If multiple packages are referencing different versions of Newtonsoft.Json , then just reference the 12.0.3 package directly in your project.如果多个包引用不同版本的Newtonsoft.Json ,则只需在项目中直接引用 12.0.3 package 即可。 At that point calling Assembly.Load("Newtonsoft.Json") should load the version you want.此时调用Assembly.Load("Newtonsoft.Json")应该加载您想要的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM