Introduction
In dotnet two or more assemblies with the same name but with different version number can exist in the GAC(Global Assembly Cache). This is the solution of DLL Hell which exists in component programming previously. I am going to provide one example for this.
1. How to create a strong named assembly with different version numbers then installing them to GAC.
2. Using Reflection how to use them in client application with different version.
Background
Elementary Knowledge of reflection is required.
Using the code
Creating DLL with version 1.0.0.0:
In Visual Studio
File -> New -> Project -> visual c# project ->Class Library.
Provide Name (I have provided MYCOM) and suitable location. Press OK button.
Change the name of class1 as Person and add one method Display.
using System;
namespace MYCOM
{
public class Person
{
public Person()
{
}
public string Display()
{
return "I am person with Version 1.0.0.0";
}
}
}
From Visual Studio Command Prompt type > sn –k mykey.snk
It will generate key file. Copy the file in the same folder where DLL will be generated (project folder\bin\debug)
Now in AssemblyInfo.cs file enter
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyKeyFile("mykey.snk")]
Now build the project.
Strong named assembly MYCOM.dll will get generated in debug folder.
From Visual studio command prompt type > gacutil –i MYCOM.dll
MYCOM.dll gets installed in GAC.
Now change the Display Funtion as
public string Display()
{
return "I am person with Version 1.0.0.1";
}
Change in Assembly.cs file
[assembly: AssemblyVersion("1.0.0.1")]
Build the project MYCOM.dll with Version number 1.0.0.1 will get generated in bin\debug folder.
From Visual studio command prompt type > gacutil –i MYCOM.dll
MYCOM.dll gets installed in GAC.
Now GAC contains two MYCOM.dll but with different version.
Client Application
In Visual Studio
File -> New -> Project -> visual c# project ->Console Application
Press OK.
Now type the following lines
using System;
using System.Reflection;
using System.Globalization;
using System.Configuration;
namespace ConsoleApplication1
{
class Class1
{
public static void Main(string[] args)
{
Assembly objm = Assembly.Load("MYCOM, Version=1.0.0.0,
Culture=neutral, PublicKeyToken = 5b5aaf21705d336b");
Type objmycom = objm.GetType("MYCOM.Person",true,true);
BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public
| BindingFlags.Static | BindingFlags.Instance |
BindingFlags.DeclaredOnly);
MethodInfo m = objmycom.GetMethod("Display",flags);
object myobj = Activator.CreateInstance(objmycom,false);
object result = m.Invoke(myobj,BindingFlags.InvokeMethod, null,
null, CultureInfo.CurrentCulture);
Console.WriteLine("result.ToString());
Console.Read();
}
}
}
You can get PublicKeyToken from GAC.
From Visual Studio command Prompt type >mscorcfg.msc

Click on view list of assemblies in the assembly cache. Right click mouse on your assembly (MYCOM) select correct version 1.0.0.0à select Properties and the copy the public key token from there and paste in your assembly name.
Assembly objm = Assembly.Load("MYCOM, Version=1.0.0.0, Culture=neutral, PublicKeyToken = 5b5aaf21705d336b");
Now execute you will see the output
"I am person with Version 1.0.0.0";
And if you change the Version as 1.0.0.1 then output will be
"I am person with Version 1.0.0.1";