Search code examples
visual-studio-2013msbuild.net-assemblymscorlib

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?


Solution

  • 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.