5

To resolve a namespace conflict between assemblies in C#, I gave one an Alias of NLog:

enter image description here

Now, I can refer to a namespace within that assembly like so:

public class Logger : NLog::NLog.LogReceiverService
{
   // ...
}

However, this won't compile until I use the extern alias keyword at the top of my file:

extern alias NLog;

My Question:

What purpose does the extern alias keyword serve? Shouldn't NLog::NLog.LogReceiverService be sufficient to fully disambiguate the assembly alias, namespace and type I'm referring to?

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • 1
    *Note:* Yes, there are many StackOverflow posts as well as blog articles that outline how to disambiguate two assemblies with the same fully qualified type. All of them basically say *Use the extern alias keyword*, but doesn't say *why*. My question is why the compiler needs the *extern alias* keyword when the `Alias::` should be enough. – Mike Christensen Jun 05 '14 at 17:31

1 Answers1

4

Setting the Aliases property on the reference will only designate it as that alias while the initial load of assembly references is done for your project as run time. In order to use that aliased assembly reference in your file, you must specify that you are going to use it via the extern alias keyword, which sets the compiler directives to look for that alias.

You would think that they would have some type of global aliasing, but I am thinking they force the explicit alias use due to a performance hit that is incurred on the alias.

Here is the reference material on extern alias: http://msdn.microsoft.com/en-us/library/ms173212.aspx

Flexo
  • 87,323
  • 22
  • 191
  • 272