Example Project Source: PluginProject.zip
Because I’ve been doing a lot of work with MarcEdit and plug-ins, I thought I’d post some sample code for anyone interested in how this might work. Essentially, the sample project includes 3 parts — a host application, a set of Interfaces and a Shared library. Making this work requires a couple of important parts.
First, the host application (either the form or class), need to implement the set of interfaces. So for example, if interaction with a form in the hosted application was need, you would configure the form to implement a set of interfaces. This would look like:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace HostApp
{
public partial class Form1 : Form, HostInterfaces.IHost
{
//...
}
This implements the IHost class (link to msdn) — a generic class that allows you to
pass objects between dynamically loaded libraries. .NET includes a IScript interface that allows for scripting functionality as well.
Anyway, the interfaces are simply like delegates — they define the visible functions/methods that will be accessible to a foreign assembly. This is the simpliest file to create. It looks something like this:
using System;
using System.Collections.Generic;
using System.Text;
namespace HostInterfaces
{
public interface IHost
{
System.Windows.Forms.Label label { get;}
System.Windows.Forms.ToolStripButton AddButton(string caption);
void RemoveButton(System.Windows.Forms.ToolStripButton t);
}
}
Finally, the Dynamic assembly has the ability to work with any function/object within the host application that has been made public through the interface. For this sample project, I’ve shown how to modify a label (on the host application), add a button to a toolbar and respond to click events from that button.
The project is a simple one — but should go a long way towards showing how this works.
Cheers,
–TR
Technorati Tags: C#,plug-ins,interfaces
Comments
One response to “C# plug-ins continued — Interacting with one’s hosted application”
Thanks for this, its very well constructed. The dynamic assemblies are very easy to work with.