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....
Hey,
Thanks very much for this. This is what I was precisely searching for.
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
Nope. You’ll need to wrap up the dlls in some other code and then call
Good one. Thanks for information
Thanks Man,
Exactly what I was looking for…
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.
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 😐
i want to use the same concept for windows mobile is there any way to give reference for the dlls?
regards
kiran
Maintenance: It seems that assemblies must be signed.
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
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…
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?
Are the two dlls strong named?
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
Is there a solution to this problem then – VS IDE wont allow adding the two dlls with different versions?
PingBack from http://blueonionsoftware.com/blog.aspx?p=f0fc19c1-bb4f-4985-adcf-a9fec3a754a1
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).
Thanks! You just saved me a s***load of time.
hello
I have to Major applications and one small and both major application is using the small one with diff version of dlls . so i am not able to run both the major application simultaneously on my system . Even after running one when i run the second all dlls are overwrited . What should i do to run both the application simultaneously
Hi
Thanks for the useful post.
Please let me know how I can add these alias references in xaml code in wpf.
Thanks
i got en error when i refer the second dll with the same name but different versions. can you please provide sample application using windows forms.
Thx !
Exactly what we need for a converter (old <==> new versions 🙂 )
I have the same problem as other that have responded. I get an error when I try to include different versions of the same dll with the same name. Is there a way around that. Otherwise, how can this solution be viable?
Well, this post is strange, the same question raised by so many people, yet no answer, while so many pros…
Thank you very much !!. This is what, I am looking for…
Good article!
And, Could you explain how to use "extern alias" in the web project (asp.net)?
To add two assemblies with same name I've edited project file manually:
<Reference Include="File – New">
<HintPath>FolderAFile.dll</HintPath>
<Aliases>new</Aliases>
</Reference>
<Reference Include="File – Old">
<HintPath>FolderBFile.dll</HintPath>
<Aliases>old</Aliases>
</Reference>
To make this last example work, we also had to use AssemblyResolve to load the correct dll from file, otherwise it would not know how to load the correct assembly from the executing directory. To make this work, we copied two dll files into the executing directory and renamed each with an appropriate version number to differentiate the two dlls.
steveb is correct, I answered this at StackOverflow, credit goes to him.
stackoverflow.com/…/22486097
thank you for this article , i have th esame problem , but I Wonder where excecute csc /r:oldVer=SomepathClassLibrary.dll /r:newVer=AnotherPathClassLibrary.dll program"
hi m using itextcsharp dll and there is not alias name given for it
Hai…
When i try to add two dll with different alias name as oldver and newver, adding refernces is not working for second dll. There is name conflicts.Can anyone please help me to recover from the above problem please guys.
Thanks in advance…
Hi, i'm dealing with this problem right now, i have made this work in most places in my application but i still have some places where i can't get it done. Can anyone provide help? My mail is santiagojlevy at hot mail dot com
Thanks in advance.
P.s. : What? My comment is not spam. -.-