Instantiating assemblies in run-time.

  • Thread starter Thread starter pesso
  • Start date Start date
P

pesso

I have an array of filenames, which are paths to the .NET assembly DLLs I
built. I know for sure that these assemblies have public method, Execute().

In the run-time, I want to be able to instantiate these assemblies from my
CS code and invoke the Execute function. Can I do this without setting the
reference in the compilation-time or using the "using" statement in the
code?

// Build the filename list.
ArrayList filenames = new ArrayList();
filenames.add("c:\temp\myassem1.dll");
filenames.add("c:\temp\myassem2.dll");

foreach (string filename in filenames)
{
// instantiate each assembly in the list. How?
// and call Execute function. How?
}
 
Hi pesso,

You can use Assembly class to load the dll and use Reflection to
invoke its method.

Sample code listed below:

using System;
using System.Reflection;
public class LoadInvoke
{
public static void Main(string[] args)
{
ArrayList filenames = new ArrayList();
filenames.add("c:\temp\myassem1.dll");
filenames.add("c:\temp\myassem2.dll");

foreach(filename in filenames)
{
Assembly a = Assembly.LoadFrom(filename);
Type[] mytypes = a.GetTypes();

foreach(Type t in mytypes)
{
MethodInfo mi = t.GetMethod("Execute");
Object obj = Activator.CreateInstance(t);
mi.Invoke(obj, null);
}
}
}
}

Hope this helps.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "pesso" <[email protected]>
| Newsgroups:
microsoft.public.dotnet.framework,microsoft.public.dotnet.languages.csharp
| Subject: Instantiating assemblies in run-time.
| Lines: 22
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Date: Wed, 17 Sep 2003 01:15:36 GMT
| NNTP-Posting-Host: 12.82.170.209
| X-Complaints-To: (e-mail address removed)
| X-Trace: bgtnsc04-news.ops.worldnet.att.net 1063761336 12.82.170.209
(Wed, 17 Sep 2003 01:15:36 GMT)
| NNTP-Posting-Date: Wed, 17 Sep 2003 01:15:36 GMT
| Organization: AT&T Worldnet
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!news-out.cwix.com!newsfeed.cwix.co
m!news.maxwell.syr.edu!news-out1.nntp.be!propagator2-sterling!propagator-ste
rling!news-in-sterling.nuthinbutnews.com!cyclone1.gnilink.net!wn14feed!world
net.att.net!bgtnsc04-news.ops.worldnet.att.net.POSTED!not-for-mail
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:185300
microsoft.public.dotnet.framework:53886
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I have an array of filenames, which are paths to the .NET assembly DLLs I
| built. I know for sure that these assemblies have public method,
Execute().
|
| In the run-time, I want to be able to instantiate these assemblies from my
| CS code and invoke the Execute function. Can I do this without setting the
| reference in the compilation-time or using the "using" statement in the
| code?
|
| // Build the filename list.
| ArrayList filenames = new ArrayList();
| filenames.add("c:\temp\myassem1.dll");
| filenames.add("c:\temp\myassem2.dll");
|
| foreach (string filename in filenames)
| {
| // instantiate each assembly in the list. How?
| // and call Execute function. How?
| }
|
|
|
|
|
 
Back
Top