Sharing plugins between two C# applications

  • Thread starter Thread starter Laurent Navarro
  • Start date Start date
L

Laurent Navarro

Hello,


I created a software some times ago, which uses some plugins to work
(which I coded also). I had problems to load those plugins as they used some
classes and functions which were compiled into the main executable. So I had
to compile them by hand, adding the main executable as reference to the
csc.exe command line. It worked.

Now, I would like to create another application which would use the
same plugin. If I link the old ones with my new software, I have an error
saying that the DLL could not be loaded. I use the Activator.GetInstance()
function...

My question is: is there a way to share my plugins between my
applications without having to compile them each time with a difference
reference to the main executable ?


Thank you for your help !

Laurent
 
Laurent,

The problem here is that you are referencing the main executable from
your plug in. The point of a plug in is to have the implementation of the
plug in be separate from that of the main program it is being included in.
By setting a reference to the main executable, you aren't decoupling the
program at all.

What you should do is take the functionality in the main executable that
is needed by the plug in and place it in a library. Then, have the plug in
and the executable reference the library.

Then, you should be able to use the plug in in each application just
fine.

Hope this helps.
 
I think the proper way to support application plugins is for the
application to define Interfaces which the plugins must implement.
Then the application needs to know that the plugin exists, so it can
load it. The loading should be generic though, so that all plugins
must implement some kind of interface defined by the application.

In order to share plugins, both your applications MUST specify the same
interfaces. So you'll likely need to move the interfaces to a seperate
assembly, and each application will reference the assembly which
comtains the interfaces.

Your plugins would ONLY reference this assembly which contains the
interface definitions.

HTH
Andy
 
Thanks Nicholas and Andy for your quick answer, I understand that I must
create an assembly that both the plugins and the applications will reference
when compiling.

I already created an abstract PlugIn class which is the base class for
all of my plugin (it is not only an interface as I have some functions in
it). Because I want my plugin to interact directly with the GUI of my
applications, I have a reference to my main form in the PlugIn class.

Of course, when I try to compile my abstract PlugIn class into an
assembly, it doesn't want because he can't find the reference to my main
form.

I'm begining in assemblies, what should I do ?

Thanks !
 
You have to rework the abstract class so that a refernce to the main
form is no longer required.

If there is something unique to your main form that's not in the normal
Form class, you could create an interface in your plugin definition
assembly, and have your plugin require it be passed an instance object
which implements that interface. Your main applicaton already has a
reference to this shared plugin definition assembly, so you can easily
make your form implement this interface. That will decouple
everything..

Andy
 
Laurent Navarro said:
I already created an abstract PlugIn class which is the base class for
all of my plugin (it is not only an interface as I have some functions in
it). Because I want my plugin to interact directly with the GUI of my
applications, I have a reference to my main form in the PlugIn class.

Of course, when I try to compile my abstract PlugIn class into an
assembly, it doesn't want because he can't find the reference to my main
form.

You can define an interface that the main form implements, and the interface
definition can be separated from the main application. Then at runtime you
pass your form instance, which implements the interface, to your plugin.

-- Alan
 
Laurent,

You should still do what I recommended.

What you need to do is create an interface that the main form will
implement which exposes the functionality. Then, you pass that interface
definition to your plug in. Of course, the interface is defined in the
assembly that is referenced by the plug in and the main application.

Either that, or pass the type System.Windows.Forms.Form to your plug in,
instead of the typed version.

It's actually a really bad idea to allow plug ins free reign like this.
You should have a constrained set of functionality that the form implements
and exposes through the interface definition and then pass that to your plug
in.
 
Back
Top