Adding Different Version of DLL in Same Project

This can be an interview question and a real life problem in developer’s life as well. Now the question is what to do in this scenario?

Let us consider an example regarding the same first:

I have a sample code for two Dll’s which resembles a lot. I mean everything is same. You can say that one is release1 and one is release2.Now I have to use both of them in my project. As I am using some functionality of release1 and some functionality of release2.

Look at the below code:

Release1 Code

  1. namespace DoIt  
  2. {  
  3.     public class LetsDoIt  
  4.     {  
  5.         public string HelloWorld(string name)  
  6.         {  
  7.             return "Hello" + name;  
  8.         }  
  9.     }  
  10. }  
Release2 Code
  1. namespace DoIt  
  2. {  
  3.     public class LetDoIt  
  4.     {  
  5.         public string SayHello(string name)  
  6.         {  
  7.             return "Hello" + name;  
  8.         }  
  9.         public void DoTask()  
  10.         {  
  11.             //Do something  
  12.         }  
  13.     }  
  14. }  
Note: The above code is just a sampel.You can change according to the requirement.

Now to achieve the same you have to follow below approach.
  1. extern alias NewRelease;  
  2. extern alias OldRelease;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Text;  
  7. namespace ConsoleApplication3  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Console.WriteLine(OldRelease::Library.Class.MethodName());  
  14.             Console.WriteLine(NewRelease::Library.Class.MethodName());  
  15.         }  
  16.     }  
  17. }  
We can do it using the command line as follows:

csc /r:OldRelease=YourPathForOldDLL\ClassLibrary.dll /r:NewRelease=YourPathForOldDLL\ClassLibrary.dll program.cs

The same thing can also be achieved 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.
Next Recommended Reading How to Find CLSID of a DLL