|
-
Jun 29th, 2006, 05:41 PM
#1
Thread Starter
PowerPoster
[2.0] plugin events
I know how to (almost) create a plugin
you need to create an interface which a plugin implements.
You also need to create an interface which allows the "host" to be known to the plugin (your application)
thats all cool.
But now, how do I register events from the plugin to the main application (host) so that if I raise an event in the host, the plugins who have these events will also be notified/alerted/executed of the event?
I have a delegate which has a string signature:
public delegate void DoSomething(string text);
event:
public event DoSomething OnEventDoSomething;
the event is in the plugin, and an eventhandler has been created.
but now after loading the assembly/instantiating the plugin from the host, how can I register the event and execute it?
Thanks
-
Jun 30th, 2006, 12:16 PM
#2
Re: [2.0] plugin events
=) I was working on this exactly but had to place it on hold. It appears that you can simply declare the event handler in the interface like usual, override it and then call it from the master just as you normally would, from what I've seen.
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Jun 30th, 2006, 12:22 PM
#3
Thread Starter
PowerPoster
Re: [2.0] plugin events
yeh thats right, which is what ive discovered but now im trying to figure out how to raise the event from the app domain (host/main app) so the plugin's can act upon th event in their way
-
Jun 30th, 2006, 12:35 PM
#4
Re: [2.0] plugin events
The "gotcha" here is that an event must be raised by the class in which it is declared.
So if you have a "Host" class with the event "OnSomething", you cannot raise this event from your "MyProgram" class.
If you're having problems syntactically raising events, the syntax is like so:
Code:
// the signature for the handler.
public delegate void SomethingEventDelegate(int arg1, string arg2);
public SomethingEventDelegate SomeEvent;
// ...
// if there are handlers "connected" to this event
if (SomeEvent != null)
SomeEvent(42, "hello");
// ...
// some class subscribes to the event.
otherClass.SomeEvent += new SomethingEventDelegate(myHandler);
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Jun 30th, 2006, 12:38 PM
#5
Thread Starter
PowerPoster
Re: [2.0] plugin events
huh? lol
so if you have an event in your application domain (main program) and you raise it, and this event is also declared in an interface which a plugin has included, is there no way to raise this event from the main app so that the plugin can listen to it and act upon it?
-
Jun 30th, 2006, 12:57 PM
#6
Re: [2.0] plugin events
Well, you could chain the events --
the plugin can subscribe to a proxy event in the host object.
the host object can subscribe to an event which will be raised by the main application. When this event is fired, the handler in the host object will simply raise it's proxy event to pass the event on to the plugin.
Another option is to have your host have a function like "GetMainClass()" which returns an object of your main class. Then your plugin can manipulate this object directly. The issue here is that the plugin has to "know" about the main class - so you need to have it in an assembly that the plugin can reference.
For a project of mine, a chat client, the I had a class layout like this:
MainAssembly (dll) - contains the ChatClient class.
MainProgram (exe) - simply instantiates a ChatClient.
PluginInterface (dll) - contains the IPlugin interface, and the host object.
Plugins (dll) - reference MainAssembly and PluginInterface in order to interact with the main object.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Jun 30th, 2006, 12:59 PM
#7
Thread Starter
PowerPoster
Re: [2.0] plugin events
complicated im never gonna get this
-
Jun 30th, 2006, 02:35 PM
#8
Thread Starter
PowerPoster
Re: [2.0] plugin events
after having some food.... lets see how it goes.
This is the code I have
//Interface (IPlugin)
Code:
public delegate void DoSomething(string text);
..
string TheAuthor { get; }
string TheDescription { get; }
IPluginHost TheHost { get; set; }
event DoSomething OnEventDoSomething;
void DoInitialize();
void DoRegister(IPlugin thePlugin);
void DoShutdown();
//Plugin
Code:
public class ThePlugin : IPlugin
..
//properties here
..
public event DoSomething OnEventDoSomething;
void DoInitialize();
{
this.OnEventDoSomething += new DoSomething(ThePlugin_OnEventDoSomething);
}
void ThePlugin_OnEventDoSomething(string text)
{
Console.Write("Event raised in the plugin, msg: " + text);
}
//Main Application (Host)
Code:
....
//instantiated Plugin
IPlugin thePlugin = (IPlugin)Activator.CreateInstance(theType);
this.DoRegister(thePlugin);
thePlugin.DoInitialize();
this.theCollectionOfPlugins.Add(thePlugin);
//here for example, how do I now raise an event of this plugin so that the plugin(s) can catch it and act on it?
I hope that helps
-
Aug 8th, 2006, 12:08 PM
#9
Member
Re: [2.0] plugin events
 Originally Posted by sunburnt
the plugin can subscribe to a proxy event in the host object.
the host object can subscribe to an event which will be raised by the main application. When this event is fired, the handler in the host object will simply raise it's proxy event to pass the event on to the plugin.
Any idea of how to implement that? Or somewhere explains that?
Thanks
-
Aug 8th, 2006, 06:56 PM
#10
Member
Re: [2.0] plugin events
noone?
i understand the idea, but don't know how to do this on c#. please, any suggestion?
Thanks,
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|