Archive for the C# 2.0 New Features Category

Assembly aliases

Posted in C# 2.0 New Features on June 22, 2008 by vasudevan

This is the first in series of articles that I’m going to write on new not so familiar C# 2.0 language enhancements.

This article will talk about the new namespace/class alias feature extremely useful in situations that I have listed below:

1. You have a third party library which happens to have namespaces and the classes that are identical to the ones you have created. When referencing both the assemblies in your client code, you may be confused on how to call the methods in each of the classes separately. You may argue that you cannot modify the namespace/classses in the third party tool but you can do so in your library. But, the point to illustrate here is C# 2.0 has ways to continue using both the libraries untouched with a new alias feature.

2. What if you have two different versions of your assemblies and both have the same namespace/classes and you wanted to use both of them for backward compatibilities or for other things. Enter C# 2.0’s new alias feature.

Here is a working code snippet that you can copy and paste:

Assembly 1:
========


using System;
using System.Collections.Generic;
using System.Text;
namespace Alias
{
    public class AliasClass
    {
        public string Method1()
        {
            return "This is from method1";
        }
    }
}

Assembly 2:
========

using System;
using System.Collections.Generic;
using System.Text;
namespace Alias
{
    public class AliasClass
    {
        public string Method2()
        {
            return "This is from method2";
        }
    }
}

AliasClient
========

using System;
using System.Collections.Generic;
using System.Text;
using Alias;

namespace AliasClient
{
    class Program
    {
        static void Main(string[] args)
        {
            AliasClass al = new AliasClass();
        }
    }
}
 


Referencing both the dlls and compiling the code, will throw “The type ‘Alias.AliasClass’ exists in both assemblies” error. Also, when you type al. the Intellisense only lists Method1() here. So, you are unable to use both the assemblies.
 

But, C# 2.0 has a very useful feature to eliminate this issue:


extern alias class1;
extern alias class2;

using System;
using System.Collections.Generic;
using System.Text;

namespace AliasClient
{
    class Program
    {
        static void Main(string[] args)
        {
            class1::Alias.AliasClass al1 = new class1::Alias.AliasClass();
            class2::Alias.AliasClass al2 = new class2::Alias.AliasClass();
            Console.WriteLine(al1.Method1());
            Console.WriteLine(al2.Method2());
            Console.ReadLine();
}
}
}


Using the new extern alias keyword, you first declare variables (“class1” and “class2”) for each of the conflicting assemblies.  Since you are within the VS.NET IDE, open the properties window for each of the assemblies and change the Aliases property to “class1” and “class2”. The important way to call the methods in the two different assemblies is to use the “::” syntax instead of the regular “.”syntax.

So, the following code will do the trick:

            class1::Alias.AliasClass al1 = new class1::Alias.AliasClass();
            class2::Alias.AliasClass al2 = new class2::Alias.AliasClass();
            Console.WriteLine(al1.Method1());
            Console.WriteLine(al2.Method2());
            Console.ReadLine();

Pretty neat…uuh…

 

 

Advertisements