Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio asks me to reference a nonexistent assembly

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?

like image 385
Skiminok Avatar asked Jan 16 '15 01:01

Skiminok


2 Answers

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>
like image 128
HFloyd Avatar answered Oct 05 '22 12:10

HFloyd


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.

like image 25
CoolMcGrrr Avatar answered Oct 05 '22 10:10

CoolMcGrrr