index > Visual C# General > Plugin events C#

Plugin events C#

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.




Need 2 be back @ MS - MS All the way! Follower since 1995 MS Super Evangelist| MSDN Forums Moderator
ahmedilyas
The exact way to do this is explained in this blog post on my blog. Although there's no explicit plugin characteristics, I think it will help you to complete your code.


Mark the best replies as answers. - http://bloggingabout.net/blogs/rick
Rick van den Bosch

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




This posting is provided "AS IS" with no warranties, and confers no rights.
Peter Huang - MSFT

Many thanks for your valuable replies

 

I shall give this a shot and come back if I have any problems.

 

Thank-you! :)




Need 2 be back @ MS - MS All the way! Follower since 1995 MS Super Evangelist| MSDN Forums Moderator
ahmedilyas

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!




Need 2 be back @ MS - MS All the way! Follower since 1995 MS Super Evangelist| MSDN Forums Moderator
ahmedilyas

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




This posting is provided "AS IS" with no warranties, and confers no rights.
Peter Huang - MSFT
Thank-you very much Peter, I greatly appreciate your valuable time and reply and has cleared up things further :)


Need 2 be back @ MS - MS All the way! Follower since 1995 MS Super Evangelist| MSDN Forums Moderator
ahmedilyas
reply 7

You can use google to search for other answers

 

More Articles

• VS2005 look for Strip-Controls?
• ASP DataGrid event handling
• print preview
• How to open a pdf document on a winform in c# ?
• Calling c# assembly from Excel 2003
• Dynamic form loading with parameters
• How to avoid page call back in ASP .NET 1.0
• How to change the Title Bar appearance??
• Flushing datatable to an Excel file usin InterOp
• Exception Handling Application Block
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• convert help
• Disabling Text boxes
• Simple windows forms question
• Preventing decompilation!!
• How can I focus / select a control on an
• Mutex.Dispose() - Does it release the mu
• Why can not close port of the serial com
• How to get a client's IP Address
• RaftingContainer upgrade to Beta 2
• Invoke a WebService automatically (using
• Scroll Panels
• DataBindig SQL - lesson 9 - missing part
• How do i create hidden Form section ?
• Help!!! on Assembly
• Printing to a dot matrix printer without

Hot Articles

• Device detection
• attempting to use an array across a class
• Get System Information
• Distribution and .Net Framework
• How to get folders opened by explorer.exe
• Issue looping through For Loop
• System.Net.Mail
• hiding explorer bar
• Create 2D line charts in C#
• First chance exceptions
• Command Line arguments make me say arrg
• Publish... Application Files...
• Books to recommend ?
• default()
• C Sharp Rockie

Recommend Articles

• Memory leak
• Change default audio output when playing
• how i can give the regex class a Timeout
• Dataset - 2003 code no longer works with
• Raw commands to Parallel Ports on USB/PC
• listbox?
• How to Get One Millisecond Timer?
• how do i convert days into months
• How to consume web method using http post?
• datagrid prevnt click
• ListBox DataSource
• Flushing datatable to an Excel file usin
• DataGridView.Rows.Add... problem!
• Can't read a zero value into a double
• How to POST data and retrive the html re