If you have 2 types with the same name (note that the name includes a namespace), but in different DLLs, and you are interested in using both of them, you can do this.
Short answer
You have an Acme.Foo type in two different DLLs and you want to use them. Give the link to the alias in the help properties window (View | Properties Window), then use it as follows:
extern alias TheAliasYouGaveTheReference TheAliasYouGaveTheReference::Acme.Foo f = new TheAliasYouGaveTheReference::Acme.Foo();
The default namespace is global for any C # program , but note that we use the alias that we created instead of global .
The best approach is not to get into this situation, first of all, if both assemblies are your own, then do not create 2 types with the same name in the same namespace. But sometimes we do not control the source code, so for those times, the above solution can be used.
Long answer
I copy most of the article from here , so it is written here if the article is no longer available.
How do you get into this situation?
First, here is how you can replicate the script, so it’s really clear what we are talking about:
- Create a C # class library called FooVersion1
Replace the template code in Class1.cs as follows:
using System; namespace Acme { public class Foo { public void Bar() { Console.WriteLine("Bar"); } } }
Right-click on a solution in Solution Explorer and select Add | New project
- Save current project (applicable only in express)
- Select the class library in the new project dialog and change the project name to FooVersion2 and click OK
Replace the code in Class1.cs as follows:
using System; namespace Acme { public class Foo { public void Bar() { Console.WriteLine("Bar"); } public void Goo() { Console.WriteLine("Goo"); } } }
Using a Type in an Application
So now we have two different assemblies containing Acme.Foo . Now create a console application and try to use each.
Create a solution using Ctrl + Shift + B (or F6) Note that you get two build errors [as shown below]:

Correction
Here's how we can fix it:
- Open Solution Manager and select FooVersion1 in the Links folder of the Consumer project
- Press F4 (or select View | Properties Window)
- Change the Aliases property to FooVersion1
- Create a solution
- Now everything will be built correctly, because Acme.Foo definitely refers to FooVersion2
Add the following directive to the beginning of the Program.cs program in the Consumer project:
extern alias FooVersion1;
Change the use of Acme.Foo to:
FooVersion1::Acme.Foo f = new FooVersion1::Acme.Foo(); f.Bar();
Please note that when you enter "f. The completion list contains only those methods in FooVersion1 Acme.Foo (in particular, it does not include Goo)
- Build a solution and everything will be built correctly.
Finally, add the following code in the f.Bar () file to the Consumer project's Program.cs:
Acme.Foo f2 = new Acme.Foo(); f2.Goo();
Note that the f2s completion list contains Goo.
- Again, create with Ctrl + Shift + B and note that there are no build errors.
CodingYoshi Jul 26 '17 at 16:40 2017-07-26 16:40
source share