two different DLL with same namespace
I have two DLL files that have the same namespace but they have different methods and types. How can I reference both DLLs in my project and use their methods and types?
By the way, these two DLLs have some methods and types with the same name but different implementation and some unique methods and types.
Solution 1:
There's nothing special you need to do - just reference them and use the types. Namespaces can span accross several assemblies without problems, because they're not really opaque types. A namespace is just a way of adding a common prefix to all the types it contains, allowing you to have multiple types of the same name under different namespaces. (The framework doesn't see them as having the same names, because it sees the "fully qualified" name of everything - which has an alias and a namespace attached to the front of it.)
In the rare event that you reference 2 assemblies which have the same type names and the same namespaces (such as 2 different versions of the same dll) - you can distinguish which assembly to use for a given type using an alias. The default alias for all references is global
, but you can specify your own alias for any assembly when you reference it (using a compiler switch - or just use the properties box in Visual Studio) - and have an extern alias <name>
clause at the top of your code file where you use it - you would access the types from different assemblies with <name>::MyNamespace.Type
Solution 2:
If you have 2 types with the exact same name (note that the name includes the namespace) but in different DLLs and you are interested in using both of them, then you can do this.
Short Answer
You have type Acme.Foo
in 2 different DLLs and you want to use them. Give the reference an alias in the reference properties window (View | Properties Window) then use it like this:
extern alias TheAliasYouGaveTheReference
TheAliasYouGaveTheReference::Acme.Foo f = new
TheAliasYouGaveTheReference::Acme.Foo();
The default namespace is global
for any C# program but note above we are using the alias we created instead of global
.
The best approach is to NOT get into a situation like this in the first place, if both assemblies are your own, then do not create 2 types with the exact same name in the exact same namespace. But sometimes we do not control the source code so for those times, the above solution can be used.
Long Answer
I am copying most of the article from here so it is recorded here in case the article is no longer available.
How do you get into a situation like this?
Firstly, here is how you can replicate the scenario so it is really clear what we are talking about:
- Create a C# Class Library called FooVersion1
-
Replace the template code in Class1.cs with the following:
using System; namespace Acme { public class Foo { public void Bar() { Console.WriteLine("Bar"); } } }
Right-click on the solution in solution explorer and select Add | New Project
- Save the current project (only applicable in express)
- Select a Class Library in the new project dialog and change the project name to FooVersion2 and press OK
-
Replace the code in Class1.cs with the following:
using System; namespace Acme { public class Foo { public void Bar() { Console.WriteLine("Bar"); } public void Goo() { Console.WriteLine("Goo"); } } }
Usage of the type in Application
Ok so now we have 2 different assemblies containing Acme.Foo
. Let's now create a console application and try to use each one.
- Right-click on the solution in solution explorer and select Add | New Project
- Select a Console Application and call it Consumer
- Right-click on Consumer and select ‘Set as startup project’
- Right-click on the references node in the Consumer project and select ‘Add Reference’
- Click on the projects tab, and multi-select FooVersion1 and FooVersion2 Click OK
-
Add the following line to Main in the Program type of the Consumer project:
Acme.Foo f = new Acme.Foo();
Build the solution via Ctrl+Shift+B (or F6) Notice that you get two build errors [as shown below]:
The Fix
Here is how we can fix it:
- Open solution explorer and select FooVersion1 in the References folder of the Consumer project
- Hit F4 (or select View | Properties Window)
- Change the Aliases property to FooVersion1
- Build the solution
- Now everything will build correctly, because Acme.Foo unambiguously refers to FooVersion2
-
Add the following directive to the top of Program.cs in the Consumer project:
extern alias FooVersion1;
-
Change the usage of Acme.Foo to:
FooVersion1::Acme.Foo f = new FooVersion1::Acme.Foo(); f.Bar();
Notice that when you type ‘f.’ the completion list contains only those methods in FooVersion1 of Acme.Foo (notably it does not include Goo)
- Build the solution and everything will build correctly
-
Finally add the following code under f.Bar() in Program.cs of the Consumer project:
Acme.Foo f2 = new Acme.Foo(); f2.Goo();
Notice that f2’s completion list contains Goo.
- Build again using Ctrl+Shift+B and notice that there are still no build errors