Using Reference Aliases

I recently ran across a situation where two different libraries shared identical namespaces and class names. Visual Studio complained anytime I used one of these classes that it was an ambiguous reference. I needed both libraries -- each contained some unique elements -- so I couldn't ditch one or the other. But, as I said, anytime I tried to use a class that was common (again, via the class name and namespace), I received the ambiguous reference error.

This was a real problem, so say the least.

However, C# provides a way to specify exactly which library (reference) you mean when you use an object that's common to two or more libraries. Simply put, you use an extern alias statement.

The concept is fairly simple:

  • Give the reference an alias (I used the Properties windows for the reference).
  • Add an extern alias statement, putting it before your using statements.
  • Add a using statement, which effectively links a specific object through the extern alias statement to the correct reference (library).

I needed an attempt or two before I got this right, because I was trying to perform the last step against a namespace rather than a class. Once I got past that hangup, though, it was smooth sailing.

The steps below show exactly what I did.

From within Visual Studio, go to the Solution Explorer window, find the project that contains the references in which you're interested, expand the References, and find the reference you want.
Click on that reference.
Review the Properties window and locate the property "Alias":

By default, the alias for a reference is "global". We're going to change this to something different. In our case, we're going to give this reference the alias "ConfigAlias":

Now that we have an alias for this reference, we have to tell our code about this alias, and then put the alias to work.

Below is a simple console application:

extern alias ConfigAlias;
using MyConfig = ConfigAlias::System.Configuration;

namespace MyAliasApp.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Starting...");

            string basePath = MyConfig::ConfigurationManager.AppSettings["Folder"];
            System.Console.WriteLine("Folder = " + basePath);

            System.Console.WriteLine("Press any key to quit...");
            System.Console.ReadLine();
        }
    }
}

Note the following:

  • The very first line:
         extern alias ConfigAlias;
    tells our code that we'll be referring to the alias ConfigAlias
  • The using statement:
         using MyConfig = ConfigAlias::System.Configuration
    tells our code that anytime we want to use class System.Configuration, we'll refer to it as "MyConfig".
  • To access a subclass of System.Configuration (now referred to as MyConfig), we'll use shorthand MyConfig, like this:
         MyConfig::ConfigurationManater.AppSettings["Folder]
The example above is simple but it illustrates another cool trick: Using the using statement to create a shorthand way to refer to a class or namespace. This comes in handy when, for whatever reason, your code requires the fully qualified namespace and class name and it's just way too long to type conveniently over and over.

And, of course, the example above doesn't show two competing libraries. But if you use the technique above for one of the references, then you'll take care of the problem.

And, believe me, if you ever have to code Bouncy Castle stuff alongside of BouncyCastle FIPS stuff, this technique will come in handy. ;)

Comments

Popular posts from this blog

List of Visual Studio Keyboard Shortcuts