Welcome to MSDN Blogs Sign in | Join | Help

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

extern alias oldVer;

extern alias 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....

Published Wednesday, November 30, 2005 2:53 PM by abhinaba
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

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

Monday, May 15, 2006 9:22 AM by Pavan Kulkarni
Hey,
Thanks very much for this. This is what I was precisely searching for.

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

Thursday, July 20, 2006 3:32 PM by Venu
Is there any way we can achieve the functionality provided by alias in c# 2.0 using the earlier version of c# (1.2)?

Thanks,
Venu

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

Friday, July 21, 2006 1:00 AM by abhinaba
Nope. You'll need to wrap up the dlls in some other code and then call

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

Monday, January 08, 2007 5:06 AM by Suresh

Good one. Thanks for information

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

Thursday, February 22, 2007 5:37 PM by Usman

Thanks Man,

Exactly what I was looking for...

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

Friday, December 07, 2007 2:00 AM by ramana reddy

its really good.i was faced the same problem when i am trying to 9.1 & 10.0 version of same assembly in my application.

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

Tuesday, June 24, 2008 10:50 AM by Maintenance

Thanks for the info..

How do I reference two different versions of the same DLL?

if I try to add a second version, visual studio throws up an error message saying that a reference to that component already exists in the project :|

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

Thursday, August 07, 2008 6:33 AM by kiran

i want to use the same concept for windows mobile is there any way to give reference for the dlls?

regards

kiran

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

Wednesday, September 03, 2008 7:09 AM by yol

Maintenance: It seems that assemblies must be signed.

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

Thursday, September 11, 2008 4:07 PM by Amitesh

Hi

This is the exact kinda solution I was looking for but the problem I have is how would i add the two dlls reference when they both have the same name. In my case I cannot rename one of the dlls. They both need to have the same name but are different versions

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

Friday, September 12, 2008 2:30 AM by abhinaba

Aimtesh why do you have to rename? Just use the same named dll from two different folders. Obviously two dlls with the same name cannot be in the same folder...

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

Saturday, September 13, 2008 10:38 AM by Pete

I'm with Amitesh, if I try to add a different version of the same dll (located in a different folder) I get an error from .NET.  "A reference to the component xxxx already exists in the project.".

Any ideas?

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

Sunday, September 14, 2008 7:24 AM by abhinaba

Are the two dlls strong named?

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

Sunday, September 14, 2008 4:37 PM by Amitesh

Yes both the dlls have strong name but still does not work. Both the dlls are in separate folders but when u try and add reference to the dlls via the VS IDE, it produces an error

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

Saturday, December 27, 2008 5:23 AM by Darshat

Is there a solution to this problem then - VS IDE wont allow adding the two dlls with different versions?

# Favorite C# Idioms | Blue Onion Software *

Friday, January 02, 2009 9:53 AM by Favorite C# Idioms | Blue Onion Software *

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

Wednesday, June 03, 2009 3:22 AM by Armin

Perfect! The thing I was looking for. I need to write a software that runs on AutoCAD 2008 and 2010. They changed the DLLs but with the same naming structure and I had a hard time to get my app running on both without create two versions of my software. You had the solution.

I hope it runs as expected (the little test app I made worked fine).

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker