Posted by: Cirilo Meggiolaro | 03/18/2009

Tip of the day #155 – Extern alias

If a .NET application needs to access two assemblies that have the same full qualifier name or consumes two versions of the same assembly you will not be able to compile your application. Classes and / or methods with the same namespace and name will exist in both assemblies and based on the fact there is no way to figure out what is the correct one you will get a compiler error.

Let’s start reproducing this scenario to make it clear:

  • Create a class library project called VersionA;
  • Open the Class1.cs file and add the following code to it:

namespace Foospace
{
    public class Foo
    {
        public void M()
        {
            Console.WriteLine(“This is the first version of Foo.”);
        }
    }
}

  • Add a new class library project to your solution and name it VersionB;
  • Open the Class1.cs file and add the following code:

namespace Foospace
{
    public class Foo
    {
        public void M()
        {
            Console.WriteLine(“This is the second version of Foo.”);
        }

        public void OneMoreM()
        {
            Console.WriteLine(“I am part of VersionB assembly only.”);
        }
    }
}

  • Add a console application to the solution and add a reference to both VersionA and VersionB class libraries;
  • Open the Program.cs file and create a new instance of Foo inside the Main method;
  • Compile the application. You get an error similar to the following:

The type ‘Foospace.Foo’ exists in both ‘c:\Documents and Settings\Cirilo\Tip155\VersionA\bin\Debug\VersionA.dll’ and ‘c:\Documents and Settings\Cirilo\Tip155\VersionB\bin\Debug\VersionB.dll’ C:\Documents and Settings\Cirilo\Tip155\Tip155\Program.cs

Workaround

The workaround to this issue is to define an external alias to the assembly so you can explicitly inform the assembly you are trying to create a reference.

How to…

  • Expand the reference list on the console application that references the class libraries;
  • Click on the VersionA assembly;
  • On the properties window, change the alias from global to VersionA;
  • Click on the VersionB assembly;
  • On the properties window, change the alias from global to VersionB;
  • Now both assemblies have their own alias. Open the Program.cs file from the console application;
  • Add the alias to the beginning of the class before the using statements using the syntax: extern alias aliasName; Your code will be similar to the following:

extern alias VersionA;
extern alias VersionB;

  • From the application’s Main method, you may instantiate the Foo class just by typing the alias name as demonstrated below:

VersionA::Foospace.Foo fooA = new VersionA::Foospace.Foo();
fooA.M();

VersionB::Foospace.Foo fooB = new VersionB::Foospace.Foo();
fooB.M();
fooB.OneMoreM();

  • Compile and run the application. You will get the following output:

This is the first version of Foo.
This is the second version of Foo.
I am part of VersionB assembly only.

I have demonstrated how to add an external alias to both assemblies. You don’t need to change the alias for both assemblies. You have the option to change only one that will be accessed by the alias name like VersionA::Foospace.Foo while the unnamed one will remain available on the global level accessed by the class name only.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: