Extern alias walkthrough
Extern aliases is a feature that was introduced in VS 2005 that I don’t believe is being used very often. There are two reasons for this. First, the feature is somewhat undiscoverable, particularly if you don’t happen to be looking for it. Second, the feature itself is useful in the somewhat narrow scenario of needing to explicitly differentiate between two type names that are identical except for the assembly strong name (name, version, public key, etc.) in which they are defined.
Of course, if you happen to be in that situation this feature can prove invaluable. I wrote the walkthrough below while we were developing the feature; it should provide a reasonable starting point for how extern aliases can be used in Visual C#.
- 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");
}
}
}
- 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:
- 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
This shows a usage pattern which we expect to be relatively common when these ambiguities exist. The assembly which will be used less often will be aliased while the other will continue to exist in the global namespace (such that it doesn’t require qualification through an extern alias).
Comments
- Anonymous
September 28, 2006
Welcome to the fifth Community Convergence update. This file is published on my blog, and shortly thereafter... - Anonymous
October 09, 2006
Learn Videos and Presentations The LINQ Framework: What's New in the May CTP Anders: Chatting about LINQ - Anonymous
October 12, 2006
Great refresher. Thanks! - Anonymous
October 18, 2006
Hi, nice article. But i just wanted to know if i can define aliases in the project scope or not? I mean to say when i want to use an alias for say Class1.Class2.Class3, i need to create that alias in all the files where i want to use them. Is there any way to define an alias once in a project and use it everywhere? ResponseThe aliases themselves are project level, but in order to bring them into scope you must use the extern alias directive in every file (the same way that using aliases work). - Anonymous
November 05, 2006
This feature is indeed difficult to discover. The happier I was to actually find it on my own. Still, my happiness didn't last for long--VS2005 errors out upon build with "The extern alias 'ntmdb' was not specified in a /reference option". This is quite weird because VS2005 actually creates a call to CSS.EXE that compiles all right if copied to a console window. It might play a role that the assembly I'm referencing is an interop DLL. Any thoughts? - Anonymous
February 13, 2007
Hi Anson,Can you please tell me how we can we get aliases property in web application. I want to use Extern alias in a web application.Thanks,Rudra Roy - Anonymous
August 13, 2007
I've tried to use this method, but ran into a problem. If the two versions of the assembly have the same name, there doesn't seem to be any way of adding them to the project references. If I rename one of the assemblies, I end up with a CS1703 error, along with MSB3243 and MSB3247 warnings.As an example, try pulling in the v1.1 System.dll and the v2.0 System.dll. - Anonymous
November 25, 2007
PingBack from http://feeds.maxblog.eu/item_702438.html - Anonymous
February 19, 2008
Hi,This is definitely a great feature when needed. Just a few questions:(1) Why do the interop assemblies generated by Visual Studio (I'm using version 2005) not feature the "Aliases" property in your screen capture?(2) Why is "alias vs no-alias" not enough for the compiler to resolve the type names? Example: try to invoke a method using a type in the non-aliased assembly which also exists in the aliased assembly...(2) My most important question: does Tlbimp.exe allow to assign an alias to an interop assembly? If not, can this be done somehow?The .NET Framework guys at Microsoft could improve type resolution a bit I think. - Anonymous
December 08, 2008
I think that if these ambiguities exists, there is something wrong with your project/solution. It is not normal to have more than one occurrence of a function (name must be unique) no matter if functionality is the same or not, in a project including external libraries.In my opinion the usage must be deprecated in order to clean the out code. - Anonymous
May 17, 2009
How can I define Alias to the DLL which is referenced in a Website and not a Web Project in VS2008 - Anonymous
October 01, 2009
Thank you very much you are my HEROE!! your walkthrough also fixed the problem of the Compile error message:"The extern alias was not specified in a /reference option" - Anonymous
March 25, 2010
Thank you!!! Article was very helpful