c# - 类型 'Newtonsoft.Json.JsonConvert' 存在于 'Newtonsoft.Json.dll' 和 'NuGetApi2.dll' 中

标签 c# .net dll visual-studio-2013 nuget

我正在尝试使用

将对象动态序列化到即时窗口中
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....

的代码

更新:可在此处找到重现错误的解决方案:

https://github.com/liufa/Temp

解决方案压缩在 WebApplication1.7z 中,我使用 MVC 应用程序,重现在 HomeControllerIndex 方法中添加断点以及何时获取点击粘贴 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/

相关文章:

c# - 使用 CaSTLe Fluent Interface 注册拦截器

c# - 更改所有页面的文本方向

c# - 子应用程序中的 Asp.net MVC Catchall 路由

c# - C# DLL 可以调用调用 native C++ 静态库的 C++/CLI 托管包装器吗?

c++ - Qt - 没有使用正确的源文件

c++ - 将一个dll的Class对象传递给另一个dll c++

javascript - 有什么方法可以在我的 c# 程序中捕获从 cefsharp 中的 javascript 抛出的错误吗?

c# - 匿名方法 (C# 2.0) 和 lambda 表达式 (C# 3.0) 之间有什么区别?

.net - MEF:CompositionContainer 和部件生命周期和所有权

c# - 在 C# 中的 IAsyncEnumerable 线程安全中增加一个整数吗?