H
Henrik Heimbuerger
Hi!
I tried a lot now but I think I need some help. ;-)
What I would like to have is a plugin interface for one of my
applications. There should be an Interface (e.g. IPluginInterface) and
it should be possible to create assemblies with classes that implement
this interface.
At runtime, I would like to load all assemblies in a special directory
(say: "\plugins"), see if there are classes in them that implement
this interface and if so, get a reference to an instance of the class
that implements it.
What I managed to do is the following:
ObjectHandle oh = Activator.CreateInstanceFrom(file, classname);
IPluginInterface p = (IPluginInterface)(oh.Unwrap());
Console.WriteLine(p.getTestString());
That worked. But there are two downsides:
a) I have to know the name of the class. I would prefer if my
application wouldn't care about the name and just check whether the
class implements the interface.
b) Can I be sure that cast in the second line fails if and only if the
interface is not exactly the same as my IPluginInterface? Would it be
possible to create a plugin where this cast does not fail but one of
the following method invocations fail? By creating a new
IPluginInterface that is not compatible to my one for example?
Or do you think there may be better plugin interfaces? The examples in
the help often use MethodInfo.Invoke() for this but I don't like it. I
just want to use this interface as if the plugin were directly coded
in my application. No more IDispatch-handling please... ;-)
Thanks for your help,
Henrik
I tried a lot now but I think I need some help. ;-)
What I would like to have is a plugin interface for one of my
applications. There should be an Interface (e.g. IPluginInterface) and
it should be possible to create assemblies with classes that implement
this interface.
At runtime, I would like to load all assemblies in a special directory
(say: "\plugins"), see if there are classes in them that implement
this interface and if so, get a reference to an instance of the class
that implements it.
What I managed to do is the following:
ObjectHandle oh = Activator.CreateInstanceFrom(file, classname);
IPluginInterface p = (IPluginInterface)(oh.Unwrap());
Console.WriteLine(p.getTestString());
That worked. But there are two downsides:
a) I have to know the name of the class. I would prefer if my
application wouldn't care about the name and just check whether the
class implements the interface.
b) Can I be sure that cast in the second line fails if and only if the
interface is not exactly the same as my IPluginInterface? Would it be
possible to create a plugin where this cast does not fail but one of
the following method invocations fail? By creating a new
IPluginInterface that is not compatible to my one for example?
Or do you think there may be better plugin interfaces? The examples in
the help often use MethodInfo.Invoke() for this but I don't like it. I
just want to use this interface as if the plugin were directly coded
in my application. No more IDispatch-handling please... ;-)
Thanks for your help,
Henrik