[Answering a follow-up question in the comments to
Solution 3]
The question was about simulation of an event like a click event.
Technically, a event can be invoked only in a class where the event is declared. It is not possible to do anywhere else, not even it the derived class. This is a one of the limitations of event instances, in contrast to "regular" delegate instances, an important fool-proof feature of events.
Please see my recent answer on this topic:
Since we have multicast delegates, why do we need events?[
^].
Take for example the class
System.Windows.Forms.Control
. It has the method
InvokeOnClick
which actually invokes the
Click
event, please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokeonclick.aspx[
^].
This way, one can directly invoke the event in the base class, but only because of this method which was exposed to the derived classes (and only for them, through the
protected
access modifier) to provide some back door to simulation of the click. To use it, one should create a derived class from any of the control classes.
Conceptually, this is possible but redundant. In real development, you don't need an event itself, not an actual click. You really need to call some method normally called when an event is invoked. So, this is exactly what you could do (instead of awkward sub-classing and using
System.Windows.Forms.Control.InvokeOnClick
:
void ProcessMyButtonClick() { }
myButton.Click += (sender, eventArgs) => {
ProcessMyButtonClick();
}
ProcessMyButtonClick();
The code shown above also shows the benefits of anonymous event handler, in the lambda form, which also allowed for
type inference (note that the types of parameters are not specified, but compiler takes them from the delegate type of the event instance
Click
, this is the delegate
System.EventHandler<System.EventArgs>
).
For C# v.2, when neither lambda nor type inference were introduced, the syntax of adding an event handler would be a bit longer, but still using anonymous event handler:
myButton.Click += (object sender, System.EventArgs eventArgs) {
ProcessMyButtonClick();
}
I'm sure that .NET prior v.2.0 and C# prior to v.2 should not be ever considered.
Now, the original question was about invocation of event between different host linked via network. After the above explanations, is should be clear that effectively the problem is reduced to calling the same method like
ProcessMyButtonClick
in response to some command sent via network.
I would note, I don't see a whole lot of sense in it. The network communication should be implemented in a separate communication layer and isolated from the UI. It could be semantic or very abstract, not exchanging some logical data and commands. You need
loose coupling, as opposed to strong coupling; please see:
http://en.wikipedia.org/wiki/Loose_coupling[
^].
I suggest you learn and analyze applicability of the following
architectural patterns (
http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[
^]):
MVVM — Model View View Model,
http://en.wikipedia.org/wiki/Model_View_ViewModel[^],
MVC — Model-View-Controller,
http://en.wikipedia.org/wiki/Model-view-controller[^]),
MVA — Model-View-Adapter,
http://en.wikipedia.org/wiki/Model–view–adapter[^],
MVP — Model-View-Presenter,
http://en.wikipedia.org/wiki/Model-view-presenter[^].
Pay attention for the motivation of those architectures. If you understand it, you would be able to create better design ideas.
Good luck,
—SA