This was an interesting problem I ran across today. There are a lot of articles on the net regarding this happening in asp .net, however I was working on class libraries and none of the articles fit this specific situation.
This problem appears when you’ve produced a new version of a referenced assembly, but for some reason your application is still trying to use an older version. Its likely it would only occur with strongly named assemblies too, as version numbers aren’t checked for non-strongly-named assemblies.
The reason this is such a strange problem when working with class libraries is that visual studio, by default, updates all of your references when they are updated for you. So if you recompile an assembly (A) that is referenced by assembly (B), then the next time you compile (B) it will update itself to use the new version of (A). In theory then, this situation should never happen.
Except that things got a little bit more complicated. I had an assembly (A). This assembly referenced a type-assembly (B), and another assembly (C). I had recompiled both C and B and was in the process of recompiling (A). Each of these libraries had unit tests using NUnit and though they had all passed for C and B, A’s tests gave the error I mentioned above. The assembly it was talking about was assembly B.
When I recompiled B however, it should have updated the references in A for me. I also manually stripped out all of A’s references and re-added them, with no success.
The solution to this problem was hard to spot at first because it actually didn’t have anything to do with Assembly A, thats just where the symptoms showed up. Turned out, Assembly C also referenced Assembly B, and I had recompiled C first. This means that C was trying to reference the old version of B, whilst A tried to reference the new version. All that had to be done was a recompilation of C.and the problem is solved.
I guess the moral of this story is keep track of your reference tree and if you’re doing a full library-wide recompile, compile from the bottom up.