Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 595
George Mauer Avatar asked Feb 05 '09 17:02

George Mauer


People also ask

How do I reference assemblies in C#?

In the Project Designer, click the References tab. Click the Add button to open the Add Reference dialog box. In the Add Reference dialog box, select the tab indicating the type of component you want to reference. Select the components you want to reference, and then click OK.

How do you specify an assembly in a type name?

Right click the website/web application property and set Assembly name field with the intended name. Eg: If the application is named as XYZ and uses namespace naming as companyname. dept. XYZ then the application may have 2 separate dlls.

Are assembly and namespace the same?

A . Net Namespace provides the fundamental unit of logical code grouping while an assembly provides a fundamental unit of physical code grouping. Namespaces is a logical group of related classes that can be used by any other language targeting the Microsoft .


2 Answers

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 
like image 151
George Mauer Avatar answered Oct 12 '22 00:10

George Mauer


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

like image 31
Andrew Hare Avatar answered Oct 12 '22 01:10

Andrew Hare