Monday, March 28, 2011

How to reference a namespace from a specific assembly?

So here is my problem.

  • My (test) project references both Castle Windsor and Rhino Mocks.
  • I am creating a class which implements Castle.Core.Interceptor.IInterceptor from the Castle.Core.dll assembly
  • In building Rhino Mocks, Ayende used Castle.Core.Interceptor and includes the whole darn namespace inside the Rhino.Mocks.dll

So when I try to build, I get the error

The type 'Castle.Core.Interceptor.IInterceptor' exists in both 'c:...\Libraries\Rhino.Mocks.dll' and 'c:...\Libraries\Castle.Core.dll'

How then do I specify that I want to use the IInterceptor instance from the Castle.Core.dll rather than the one included in Rhino Mocks?

From stackoverflow
  • You can use an extern alias to alias one of the assemblies to prevent the ambiguity.

    George Mauer : Wow, not much info there huh? I'm not sure this applies though, it says you have to do stuff in the command line. Googling for more info...
    Andrew Hare : Sorry about that - Beardo provided a much better link
    George Mauer : This right here was even better: http://blogs.msdn.com/ansonh/archive/2006/09/27/774692.aspx
  • I believe that is answered here.

    Andrew Hare : +1 much better link - good find
  • Let's throw the specific answer up here in case someone comes along later. From article here.

    • Select one of the two assemblies under project references (in my case I selected Castle.Core). Hit F4 to bring up properties and enter alias CastleCore
    • At the top of the problematic cs file put extern alias CastleCore;
    • Reference your class with CastleCore::Castle.Core.Interceptors.IInterceptor. Or in my case I simply did:

    using cci = CastleCore::Castle.Core.Interceptors;

    and can now reference

    cci.IInterceptor
    
    Kit : Thanks for the Visual Studio-centric solution!
    James Thigpen : This should be the answer.

0 comments:

Post a Comment