Introduction
This article describes how to use DLLs which are in the GAC. The technique described is very useful to use common projects in a secure way.
Background (steps to put a DLL in GAC)
First, in the command prompt, go to the folder in which the project DLL is saved. Then, create a strong name for that DLL by using the command sn -k [strong name.snk]. Then, put that strong name in the [assembly: AssemblyKeyFile("strongnamepath")]
line in assemblinfo.cs, and build that project. Again in command prompt, type gacutil /i [assemblyname]. Now, the DLL is moved to GAC. In the code available for download, you will find out how to use the GAC DLL.
Using the code
You need to move the correct DLL to the GAC by following the above steps. In our example, PutitinGAC
is the DLL to be moved to GAC.
The first step to do to refer a DLL in GAC is to assign it to the local assembly object fromGAC
. While loading, the DLL version number and public key token should match that of the DLL in the GAC (c:\Winnt\assembly). In the DLL's properties, you can find the public key and version details.
Assembly fromGAC = Assembly.Load("PutitinGAC, " +
"Version=1.0.2292.30259, Culture=neutral," +
" PublicKeyToken=31f5625abd53197f");
Object[] oParam= {1,2};
Console.WriteLine(fromGAC.GetType(
"PutitinGAC.Class1").GetMethod("add").Invoke(fromGAC,oParam));
Points of Interest
I am excited to know more about assemblies and the features they provide. It'd be great if anybody can update this with more functionalities.
History
- Source code released: April 12 - version 1.0.