C# 2.0: Using different versions of the same dll in one application

Lot of things have become really easy to do in C#2.0. I needed to load 2 versions of the same class library dll in my application. The format of output file had changed between versions. So I need to deserialize an xml using the older version and then convert it to the newer version and then serialize it out.  So at the same time I needed to load both the old and new dlls. Lets assume that you have the following implementation of the class libraries

Older version

 using System;namespace MyLibrary{    public class MyClass    {        public static string method()        {            return "From old version ClassLibrary2";        }    }}

Newer version

 using System;namespace MyLibrary{    public class MyClass    {        public static string method()        {            return "From new version ClassLibrary2";        }    }}

The structure of both libraries are exactly the same and they have the same namespace and classes and only differ in implementation. If I add reference to both the class library dlls and try to access the methods in the client using the following code

 using System;using System.Text;namespace ClientApp{    class Program    {        static void Main(string[] args)        {            Console.WriteLine(MyLibrary.MyClass.method());        }    }}

Compilation will fail with the error "The type 'MyLibrary.MyClass' exists in both 'Somepath\ClassLibrary.dll' and 'AnotherPath\ClassLibrary.dll'". The reason being that the access becomes ambiguous. 

This is where the new namespace alias qualifier and the extern alias comes into the picture. First we write the client application (program.cs) as

 externalias oldVer; externalias newVer; using System;using System.Text;namespace ClientApp{    class Program    {        static void Main(string[] args)        {            Console.WriteLine(oldVer:: MyLibrary.MyClass.method());            Console.WriteLine(newVer:: MyLibrary.MyClass.method());        }    }}

In the code-snippet above all the important bits are marked in bold. The extern alias declaration creates a namespace parallel to the global namespace. Pre C#2.0 all namespaces were rooted at the global namespace, which is no more the case. We are telling the compiler that two other namespace oldVer and newVer exists which are externally defined. We now use the :: operator to access inside the namespace alias. We could have used the regular . qualifier but the :: qualifier ensures that the identifier on the left-hand of the :: qualifier is an extern or using alias.

The next step is to define the external alias. We can do it using the command line as follows

 csc /r:oldVer= Somepath\ClassLibrary.dll /r:newVer= AnotherPath\ClassLibrary.dll program.cs

So the contents of the older version is loaded under the oldVer alias and the new version under newVer. So all access ambiguity is removed.

The same thing can also be acheived using the VS IDE. Add the reference to both the dlls in your client application solution. Then in the Solution Explorer under the reference node select the first (old version) class library. In the property window change Aliases field from global to oldVer. Make similar change after selecting the newer class library as well. You are then good to go....