IT Community - Software Programming, Web Development and Technical Support

How to use DLLs which are in the GAC in C#. NET?

This is a discussion on How to use DLLs which are in the GAC in C#. NET? within the C# Programming forums, part of the Software Development category; How to use DLLs which are in the GAC in C#. NET?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 07-25-2007, 03:15 AM
Archer Archer is offline
D-Web Programmer
 
Join Date: Jun 2007
Posts: 52
Archer is on a distinguished road
Question How to use DLLs which are in the GAC in C#. NET?

How to use DLLs which are in the GAC in C#. NET?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-25-2007, 03:20 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default 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();
}
}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/c-programming/2700-how-use-dlls-gac-c-net.html
Posted By For Type Date
How to use DLLs which are in the GAC in C#. NET? | Web Hosting This thread Refback 02-28-2008 07:08 AM


All times are GMT -7. The time now is 11:43 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0