dynamic loading of dlls

G

Guest

Hi guys

I'm writing a console app in C# for which i want to enable the user to have
several "plug-ins". The app has a switch to set the plug-in to use, and at
runtime the required dll is loaded and executed.

I had thought I could do this with a C# class compiled, and just load the
relevant dll file for the class we want, and call it as normal. Of course
this won't work because C# class files need to be compiled into the app?
Correct?

That being the case, how do I load "proper" DLL files at runtime and call
functions from within them?

Secondly, how do I write "proper" DLLs in C# ?

Cheers



Dan
 
P

Peter Rilling

Take a look at the Assembly.LoadAssembly method and the Reflection
namespace.
 
G

Guest

Hi Dan,
I am not sure what you mean by "proper" dll but below is an example of how
you can make a plugin architecture.

I have two assemblies (i.e. dlls) one called PluginConsole which will load
the external dynamic assemblies and contains the main program and an
interface called IPlugin which describes the methods the plugin should
implement. The other assembly is called SomeLibrary which contains a class
called PlugIn1 which implement the IPlugin interface.

//IPlugin.cs inside PluginConsole.dll
using System;

namespace PluginConsole
{
public interface IPlugin
{
void DoSomething();
}
}


The class in a different dll which implements this interface
//Plugin1.cs inside SomeLibrary.dll
using System;
using PluginConsole;

namespace PluginConsole
{
class PlugnIn1 : PluginConsole.IPlugin
{
public Plugin1(){}

public void DoSomething()
{
Console.WriteLine("Plugin1 doing something");
}
}
}


The main plugin application will load the SomeLibrary.dll and then create an
instance of Plugin1. In order to do this you either need to add the
SomeLibrary assembly to the GAC or just copy it the the bin/debug (assuming
that is where you are running from) inside the PluginConsole project so that
the PluginConsole can load the assembly using the friendly name (i.e. the
name of the DLL without the .dll postfix).


//Program.cs inside PluginConsole.dll
using System;
using System.Reflection;

namespace PluginConsole
{
class Program
{
static void Main(string[] args)
{
IPlugin plugin;
Type iPluginInterface;

//Load the plugin assembly, using the friendly name
//i.e. the name of the dll without .dll
Assembly dynamicAssembly = Assembly.Load("SomeLibrary");

//Get all of the types in the assembly
Type[] types = dynamicAssembly.GetTypes();

//loop through all the types looking for one that implements
//the IPlugin interface
foreach (Type type in types)
{
//See if this type implements the interface
iPluginInterface = type.GetInterface("IPlugin");

if (iPluginInterface != null)
{
//Need to create an instance of the type -> this
implementation
//below is using the empty constructor so you must have
one.
//This is creating an instance of PlugIn1 then we cast
it to
//IPlugin so that we know how to use it.
plugin = (IPlugin)Activator.CreateInstance(type, null);

//Can call the interface methods on the type
plugin.DoSomething();
}
}
}
}
}


Hope that helps
Mark R Dawson
http://www.markdawson.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top