Visual Studio asks me to reference a nonexistent assembly

爱⌒轻易说出口 提交于 2020-02-03 03:16:45

问题


I somehow brought my project to a state where Visual Studio 2013 fails to compile it with a ridiculous error:

The type 'System.Collections.Generic.Dictionary`2' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

First of all, there is no such assembly, it does not exist. Second, Dictionary<TKey, TValue> is defined in mscorlib.dll, which is referenced by default, of course. Resharper (which has its own code analysis engine) reports that the solution should compile normally.

I don't know how on Earth it could happen, because my latest changes have nothing to do with the supposedly erroneous place at all. The line references some standard LINQ functions (GroupBy and ToDictionary), and it worked for months without any changes. Unfortunately, I cannot create any MREs: obviously, this error only appears in the context of my huge solution, and only with some specific changes made in the presumably irrelevant places.

Here's what I tried, and it didn't work:

  • Clean + Rebuild
  • Changing target Framework from .NET 4.5 to .NET 4.5.1 or .NET 4.0

Has anyone seen such a quirk before?


回答1:


Check that all the (core .net) references in your project are valid and they all reference the same .NET framework version.

If in doubt, remove all references and add them again (using the 4.0.0.0) versions.

I had this happen to me once (with the exact same error) and it was due to some "screw up" on the version of the references. I never got to find out WHY it happened, but removing all references and adding them again worked for me.




回答2:


I had the same problem, but it was on a website project - not any easy way to remove & re-add the references. Luckily, I found that this answer worked for me - and was even quicker than messing with references https://stackoverflow.com/a/29575865/3841490

adding a new assembly tag to web.config seems to have resolved this issue. The tag I added was under the tag and as follows:

<assemblies>
     <add assembly="System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
</assemblies>



回答3:


I had the exact same issue and it seemed to be related to two NuGet packages containing the same assembly "System.Collections.Immutable". The two NuGet packages where "Microsoft.Bcl.Immutable" (Unlisted) and "System.Collections.Immutable".

I haven't been able to reproduce the issue reliably, and it seems the problem was solved by using VS2015.

However, i would suggest looking for NuGet packages containing the same assembly.

Removing Microsoft.Bcl.Immutables solved this for me.



来源:https://stackoverflow.com/questions/27976099/visual-studio-asks-me-to-reference-a-nonexistent-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!