Re: How to use DLLs which are in the GAC in C#. NET? GAC-Global Assembly Cache
First step is to put our DLL in the GAC:
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 file as
[assembly: AssemblyKeyFile("strongnamepath")] line in assemblyinfo.cs and build that project. Again in command prompt, type
gacutil /i [assemblyname]
Now, the DLL is moved to GAC.
How to use GAC DLL:
The first step is to load the GAC DLL into the assembly. This can be done by the following code:
Assembly fromGAC = Assembly. Load ("PutitinGAC,” +
"Version=1.0.2292.30259, Culture=neutral," +
" PublicKeyToken=31f5625abd53197f");
Where PutitinGAC is the namespace
Assembly.Load has the assembly string as parameter.
Object [] oParam= {1, 2};
Where oParam is the object that takes the parameters for the add function.
fromGAC.GetType ("PutitinGAC.Class1").GetMethod ("add").
Invoke (fromGAC, oParam));
Where PutitinGAC in the namespace
Class1 is the class name
add is the method name
oParam is the object that takes the parameters for the add function.
Example:
Class1.cs
namespace PutitinGAC
{
public class Class1
{
public int add(int a,int b)
{
return a-b;
}
}
}
GAC DEMO:
using System;
using System.Reflection;
namespace GacDemo
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Assembly fromGAC = Assembly.Load("PutitinGAC, Version=1.0.2292.30259, Culture=neutral, PublicKeyToken=31f5625abd53197f");
Object[] oParam= {1,2};
Console.WriteLine(fromGAC.GetType("PutitinGAC.Clas s1").GetMethod("add").Invoke(fromGAC,oParam));
Console.ReadLine();
}
}
} |