我正在尝试使用
将对象动态序列化到即时窗口中Newtonsoft.Json.JsonConvert.SerializeObject(myObj);
但是我得到以下错误
The type 'Newtonsoft.Json.JsonConvert' exists in both 'Newtonsoft.Json.dll' and 'NuGetApi2.dll'
当项目中未引用的 .dll
被放置到 bin
文件夹中时,会发生在项目中引用的其他 .dll
旁边项目并在其中具有相同的库(在本例中,NuGetApi2.dll
中的 Newtonsoft.Json
未在项目中引用)
为什么我只在使用立即/调试窗口时出现此异常,而在编译代码时却没有?(编译后工作正常,因为编译器正在使用项目中引用的 dll)
如何告诉 Visual Studio 使用哪个 .dll
(最好不必停止程序运行)?
一种明显的方法是删除项目中未引用的 .dll
,....我要问的是:有没有办法告诉它要使用哪个 dll
使用来自 Immediate window
....
更新:可在此处找到重现错误的解决方案:
解决方案压缩在 WebApplication1.7z
中,我使用 MVC 应用程序,重现在 HomeController
、Index
方法中添加断点以及何时获取点击粘贴 Newtonsoft.Json.JsonConvert.SerializeObject("sfdsdfsdf");
到 Visual Studio 的即时窗口中。
项目中的代码如下:
public class HomeController : Controller
{
public ActionResult Index()
{
var x = Newtonsoft.Json.JsonConvert.SerializeObject("sdfsdf");
#if DEBUG
var y = Newtonsoft.Json.JsonConvert.SerializeObject("sfssesss");
#endif
return View(); //put breakpoint here then into immediate window paste Newtonsoft.Json.JsonConvert.SerializeObject("sdfsdf"); and you should get the error.
}
最佳答案
默认情况下,.Net 网站会在启动时加载 bin 文件夹中的所有程序集。您可以通过修改 web.config 文件来更改此行为,在这种情况下,您需要列出要加载的程序集。
这里是一个例子,只加载 1.0.0.0 版的 MySite.dll
<system.web>
<compilation>
<assemblies>
<remove assembly="*" />
<add assembly="MySite, Version=1.0.0.0, Culture=neutral />
</assemblies>
</compilation>
</system.web>
如果您只加载您需要的程序集而不加载 NuGetApi2 程序集,这应该可以解决您的问题
关于c# - 类型 'Newtonsoft.Json.JsonConvert' 存在于 'Newtonsoft.Json.dll' 和 'NuGetApi2.dll' 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39345365/