Using Identically Named Types from Different Assemblies

If you want to use two types from different assemblies that have exactly the same name, including the namespace, your application won’t know which type to use.  The fully qualified type names are identical and you’ll get an error.
You can use both types in your project by providing an extern alias for the namespace hierarchy in one of the DLLs.
In Visual Studio, in your application, select the reference to one of the two assemblies.  Change the Aliases property from global to a new name.
Now include an extern alias statement in your code, using the same name.  Then use this alias to reference types in the aliased assembly.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using DogLibrary;   // FirstDogLibrary.dll
 
namespace ConsoleApplication1
{
    extern alias AnotherLib;
 
    class Program
    {
        static void Main()
        {
            // DogLibrary.Dog in FirstDogLibrary.dll
            Dog d = new Dog("Kirby", 12);
 
            // DogLibrary.Dog in AnotherDogLibrary.dll
            AnotherLib::DogLibrary.Dog d2 = new AnotherLib::DogLibrary.Dog("JRT", "Jack");
        }
    }
}