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.d ll
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.I Plugin
{
public Plugin1(){}
public void DoSomething()
{
Console.WriteLi ne("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.d ll
using System;
using System.Reflecti on;
namespace PluginConsole
{
class Program
{
static void Main(string[] args)
{
IPlugin plugin;
Type iPluginInterfac e;
//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
iPluginInterfac e = type.GetInterfa ce("IPlugin");
if (iPluginInterfa ce != 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)Activa tor.CreateInsta nce(type, null);
//Can call the interface methods on the type
plugin.DoSometh ing();
}
}
}
}
}
Hope that helps
Mark R Dawson
http://www.markdawson.org
"dhnriversi de" wrote:
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