How to implement this new event handler system I want to invent in c# if not in what language?
I asked a previous question about event bubbling in .NET What's equivalent to javascript bubbling events in .NET?
which doesn't exist like in Javascript.
Someone said it would be due to performance reason though I'm not sure because DOM has no performance problem why would Winform.
But let's say it's true, then I'd like to create something intermediary between a systematic bubbling system like javascript 开发者_StackOverflow中文版and a non-existant bubbling system in .NET.
The purpose is as always loose coupling. That is I don't want an explicit reference in my code like using sender or hard-wire the method call with a reference to a base parent.
What I'd like is just use a keyword like "Forward".
How could I implement such system in C#. If not possible in C# what languages would allow me to do so ?
Look at how events are done in WPF including routed events. There is no need for a new keyword, just a set of classes the route the events in the way you wish.
MFC even did this sort of thing in C++ - without any "events" surport in C++ it's self.
If you want routed events like in WPF I can bet it is easier to rewrite your app into WPF than writing your own event framework. If you also like other things like two way databinding (have a look at MVVM) or improved layout customization story you should really have a look at WPF.
You could probably implement routed events in WinForms too. You "just" need to implement the emitting, consuming and bubbling parts of the framework.
I think your main problem with implementing your own event handling framework will that existing controls does not know how to emit or consume your events. You have no existing hook points in the Winforms layout where you can attach the consuming of an event.
You can implement it yourself. You'll need your own EventManager class.
public class EventManager
{
public void AddEvent(Control control,string eventName,Delegate handler);
public void RemoveEvent(Control control,string eventName,Delegate handler);
}
this class then uses a HashSet and the events ControlAdded and ControlRemoved to track all controls on which you registered an event and all their child. And then subscribes it's own helper-delegate for the registed event on all children of the control on which you registered the event. This helper delegate then calls the registered events using the bubbling rules you want.
精彩评论