Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lingering assembly dependency in C# .NET

My C# project - we'll call it the SuperUI - used to make use of a class from an external assembly. Now it doesn't, but the compiler won't let me build the project without the assembly reference in place. Let me elaborate.

This project used to throw and catch a custom exception class - the SuperException - which was derived from the standard System.Exception and lived in a separate, precompiled assembly, SuperAssembly.DLL, which I referenced.

Eventually, I decided this was a pointless exercise and replaced all SuperExceptions with a System.SuitableStandardException in each case. I removed the reference to SuperException.DLL, but am now met with the following on trying to compile the project:

The type 'SuperException' is defined in an assembly that is not referenced. You must add a reference to assembly 'SuperException, Version=1.1.0.0 (...)'

The source file referenced by the error doesn't seem relevant; it's the project namespace that gets highlighted in the IDE.

Now, here's the thing:

  1. All uses of SuperException have been eliminated from the project's code.
  2. Compared to another project that compiles fine without a reference to SuperException.DLL, I only reference one more assembly - and that references nothing that my project doesn't reference itself. While it's possible that any of these dependencies could throw SuperExceptions, I'm only catching the base Exception class and in any case... the other project builds fine!
  3. I've done Visual Studio's "Clean Solution" and cleared everything out by hand, many times.

It's not the end of the world to include this reference, I just don't see why it's necessary any more. Nrrrgg. Any pointers welcome!

like image 453
Dogmang Avatar asked Aug 12 '08 19:08

Dogmang


2 Answers

It's likely a transitive reference, where some type method call returns an instance of SuperException boxed ("downcast") as e.g. Exception, but from inspecting the code in the transitively included code, i.e. code from your external method calls, the compiler knows that you need to be able to have information about that type at some point.

Resharper would tell you where it's the case that you need to add a reference, and you could use Lütz Roeder's aka RedGate's Reflector to scan compiled IL for a reference to this type in two ways: 1) use the search-facility, 2) open each public type you're using and for that one which requires the "ghost" assembly, it will ask you to specify its location.

This most often happends to me when I reference Castle.Windsor but not Castle.MicroKernel. :p

like image 194
Henrik Avatar answered Oct 14 '22 19:10

Henrik


  1. Exit Visual Studio
  2. Delete the bin and obj Folders in your solution directory
  3. Restart and see what happens
like image 38
Michael Stum Avatar answered Oct 14 '22 18:10

Michael Stum