Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to use a DLL from the GAC

0.00/5 (No votes)
11 Apr 2006 1  
This article descibes how to use the methods in a DLL which is located in the Global Assembly Cache.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here