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
- namespace DoIt
- {
- public class LetsDoIt
- {
- public string HelloWorld(string name)
- {
- return "Hello" + name;
- }
- }
- }
Release2 Code
- namespace DoIt
- {
- public class LetDoIt
- {
- public string SayHello(string name)
- {
- return "Hello" + name;
- }
- public void DoTask()
- {
-
- }
- }
- }
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.
- extern alias NewRelease;
- extern alias OldRelease;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication3
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine(OldRelease::Library.Class.MethodName());
- Console.WriteLine(NewRelease::Library.Class.MethodName());
- }
- }
- }
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.