Plugins and Late Binding

  • Thread starter Thread starter ThunderMusic
  • Start date Start date
T

ThunderMusic

Hi,
I have some code to load some plug-ins, but the code requires me to know
the name of the class to load (here: SamplePlugin, derived from IPlugin) :

(Here is some C# code, but I use VB.Net to code my program)
using System;
using System.Reflection;

public class Driver
{
static void Main()
{
Assembly assembly = Assembly.LoadFrom ("myplugin.dll");
Type t = assembly.GetType ("SamplePlugin");
IPlugin plugin = (IPlugin) Activator.CreateInstance(t);
plugin.SayHello();
}
}

The problem is, I don't want to have to know the name of the class I want to
load... I want to load the new module and get an instance of the class that
derives IPlugin. Is there a way to do so?

In C++ for the same purpose, I had a win32 dll with an extern function that
returned an instance of the contained class, so I loaded the dll, called the
function and I was ready to proceed. Is there something similar I can do
with .NET class libraries?

thanks
 
ThunderMusic said:
I have some code to load some plug-ins, but the code requires me to know
the name of the class to load (here: SamplePlugin, derived from IPlugin) :
Assembly assembly = Assembly.LoadFrom ("myplugin.dll");
Type t = assembly.GetType ("SamplePlugin");
The problem is, I don't want to have to know the name of the class I want to
load... I want to load the new module and get an instance of the class that
derives IPlugin. Is there a way to do so?

foreach (Type Exported in assembly.GetExportedTypes())
if (Exported.IsClass && Exported.GetInterface("IPlugin", true) !=
null)
;
 
Hello,
I have 'played around' with using plugins and came across the same
issue. One way around is to create an attribute that can only be used one
and only in a '[assembly:xxxx]' level. The attribute constructor can take a
string for description and a string or a type for another. Then when you
load that assembly just look for your assembly-based attribute and you will
have an indication of the class that you can instantiate. Also, in the
construct of your attribute you could check that the type you pass it is
compatible with your IPlugin interface.

Hope this helps, If you have any problems then drop me a line!

Alan Seunarayan
 
Hello,
I have 'played around' with using plugins and came across the same
issue. One way around is to create an attribute that can only be used one
and only in a '[assembly:xxxx]' level. The attribute constructor can take a
string for description and a string or a type for another. Then when you
load that assembly just look for your assembly-based attribute and you will
have an indication of the class that you can instantiate. Also, in the
construct of your attribute you could check that the type you pass it is
compatible with your IPlugin interface.

Hope this helps, If you have any problems then drop me a line!

Alan Seunarayan
 
You could also just drop plugins in your plugin directory, get the public
class names and pick those that supports your interface...

Patrice

--

Alan Seunarayan said:
Hello,
I have 'played around' with using plugins and came across the same
issue. One way around is to create an attribute that can only be used one
and only in a '[assembly:xxxx]' level. The attribute constructor can take a
string for description and a string or a type for another. Then when you
load that assembly just look for your assembly-based attribute and you will
have an indication of the class that you can instantiate. Also, in the
construct of your attribute you could check that the type you pass it is
compatible with your IPlugin interface.

Hope this helps, If you have any problems then drop me a line!

Alan Seunarayan

Jon Shemitz said:
foreach (Type Exported in assembly.GetExportedTypes())
if (Exported.IsClass && Exported.GetInterface("IPlugin", true) !=
null)
;
ThunderMusic said:
Hi,
I have some code to load some plug-ins, but the code requires me to
know
the name of the class to load (here: SamplePlugin, derived from IPlugin) :

(Here is some C# code, but I use VB.Net to code my program)
using System;
using System.Reflection;

public class Driver
{
static void Main()
{
Assembly assembly = Assembly.LoadFrom ("myplugin.dll");
Type t = assembly.GetType ("SamplePlugin");
IPlugin plugin = (IPlugin) Activator.CreateInstance(t);
plugin.SayHello();
}
}

The problem is, I don't want to have to know the name of the class I want
to
load... I want to load the new module and get an instance of the class
that
derives IPlugin. Is there a way to do so?

In C++ for the same purpose, I had a win32 dll with an extern function
that
returned an instance of the contained class, so I loaded the dll, called
the
function and I was ready to proceed. Is there something similar I can do
with .NET class libraries?

thanks
 
Back
Top