I hope this is the correct place to post this Q. I am also going to write a tutorial/blog/experience report on this as I cannot find the correct resources on the net, believe it or not (ok so my search keywords are not great!).....
I am writing my very first plugin for my application in C#
I have created an interface. I have implemented this interface in the plugin.
I have also created a IPluginHost interface, which sets the host (application) in the plugin.
I can load/create/instantiate the plugin.
Now, I have made a delegate/event in the interface. The plugin implements this.
//delegate example: public delegate void DoSomething(string text); //event in interface and plugin: public event DoSomething OnEventDoSomething; //initialize method in plugin this.OnEventDoSomething += new DoSomething(ThePlugin_OnEventDoSomething); |
thats all fine.
BUT now, from the application host, how can I instantiate/create this event so if I raise an event in the application host, the plugins will respond to this/raise the event in the plugin.
I appreciate your guidence.

Plugin events C#
rtaiss
Dear Customer,
You can consider my code as somewhat Design Pattern issue although it seems that did not follow any pattern exactly.
>Where does the MyEventClass come in and why exactly
The class is not necessary, because we declare an interface, MyEventInterface, so we must have code to implement the interface and fire the event. We can do all of what the MyEventClass does in the Plugin. But if we want create another Plugin class that follow the interface, we have to do the code in the MyEventClass again.
>as to the host/main application - I cannot seem to see the method being implemented to fire the event.
I implement the code that fire event in the class below.
public class MyEventClass : MyEventInterface
{
public event DoSomething EventDoSomething;
public void FireEvent(string text)
{
if (null != EventDoSomething)
EventDoSomething(text);
}
}
>Why do we create the MyEventClass How will this be implemented in A plugin
>"void FireEvent(string text) { if (null != EventDoSomething) EventDoSomething(text); }"
>this is in the MyEventClass, how would this be implemented in A plugin Or would the developer have to do this, just as we do when we are raising events >>from our application
See comment 1, this is not necessary. We have implement the event in MyEventClass and our plugin inherit from the class, we do not do that again.
If you have experience in Win32, this event modal is just a callback mechanism, when we subscribe the event, we are put the function point of the event handler func into the event class. When we fire the event, we are trying to call the function point.
oPlugin.FireEvent("Fire event")
oPlugin is a reference to the plugin class which implement the interface MyEventInterface.
So the call above, will finall call the method FireEvent which will call the event handler function.
public class MyEventClass : MyEventInterface
{
public event DoSomething EventDoSomething;
public void FireEvent(string text)
{
if (null != EventDoSomething)
EventDoSomething(text); // this is how .NET event modal works in C#.
}
}
If you have confusion with event modal, please refer to the link below.
Events (C# Programming Guide)
http://msdn2.microsoft.com/en-us/library/awbftdfh.aspx
If you still have any concern, please feel free to post here.
Best regards,
Peter Huang
nullity
Many thanks for your valuable replies
I shall give this a shot and come back if I have any problems.
Thank-you! :)
Thucydides
OK I have some questions.
I don't quite follow this.
Where does the MyEventClass come in and why exactly
as to the host/main application - I cannot seem to see the method being implemented to fire the event.
This is confusing for me, I am new at this so please bare with me.
IPlugin.cs
public interface IPluginHost
{
bool DoRegister(IPlugin thePlugin);
}
public delegate void SomeDelegate(string someText);
public interface IPlugin
{
string TheDescription { get; }
string IPluginHost TheHost { get; set; }
void DoInitialize();
void DoShutdownPlugin();
event SomeDelegate OnEventDoSomething;
}
//ThePlugin.cs
public class ThePlugin : IPluginHost, IPlugin
{
private IPluginHost theHost = null;
string TheDescription get { return "Test Plugin"; }
string IPluginHost TheHost get { return this.theHost; } set { this.theHost = value; }
public event DoSomething OnEventDoSomething;
void DoRegister(IPluginHost host)
{
this.theHost = host;
}
void DoInitialize()
{
this.OnEventDoSomething += new SomeDelegate(ThePlugin_OnEventDoSomething);
}
void ThePlugin_OnEventDoSomething(string someText)
{
Console.Write("Plugin event raised: " + someText);
}
}
//MainApp/Host
..
..
IPlugin thePlugin = (IPlugin)Activator.CreateInstance(theType);
thePlugin.DoRegister(this);
thePlugin.DoInitialize();
this.theCollectionOfPlugins.Add(thePlugin);
..
that is my code. I am trying to implement and work with your solution but some things are hard to understand/follow, like the MyEventClass and raising the event from the host (FireEvent)
Why do we create the MyEventClass How will this be implemented in A plugin
"void FireEvent(string text) { if (null != EventDoSomething) EventDoSomething(text); }"
this is in the MyEventClass, how would this be implemented in A plugin Or would the developer have to do this, just as we do when we are raising events from our application
In the Host app, I see there is a "oPlugin.FireEvent("Fire event")" - how will this actually fire the event in the plugin
Sorry for the questions, its a learning curve for me and I hope to understand more and hopefully implement this in my solution.
Thanks!
KonRi
Raulsassaa
Viking_p
Dear Customer,
Here is a link for your reference about how to Implement Interface Events.
http://msdn2.microsoft.com/en-us/library/ak9w5846.aspx
Also here is a simple demo for your reference.
[Interface class]
namespace PluginDemo
{
public delegate void DoSomething(string text);
public interface MyEventInterface
{
event DoSomething EventDoSomething;
void FireEvent(string text);
}
public class MyEventClass : MyEventInterface
{
public event DoSomething EventDoSomething;
public void FireEvent(string text)
{
if (null != EventDoSomething)
EventDoSomething(text);
}
}
}
[Plugin class]
namespace PluginDemo
{
public class PluginClass : MyEventClass
{
public PluginClass()
{
this.EventDoSomething += new DoSomething(PluginClass_EventDoSomething);
}
void PluginClass_EventDoSomething(string text)
{
Console.WriteLine("Event Fired in plugin class");
Console.WriteLine(text);
}
}
}
[HostApp]
namespace PluginDemo
{
class HostApp
{
MyEventInterface oPlugin = null;
public HostApp(MyEventInterface Plugin)
{
oPlugin = Plugin;
}
public void FirePluginEvent()
{
oPlugin.FireEvent("Fire event");
}
static void Main(string[] args)
{
string plugAssemblyPath = @"path to the plugin dll";
MyEventInterface oPlugin = Activator.CreateInstanceFrom(plugAssemblyPath, "PluginDemo.PluginClass").Unwrap() as MyEventInterface;
if (null != oPlugin)
{
HostApp oHA = new HostApp(oPlugin);
oHA.FirePluginEvent();
}
else
{
Console.WriteLine("Plugin Created Failed");
}
}
}
}
Hope this helps.
Best regards,
Peter Huang