[Journal - Event Handling with .NET Console]

Event Handling with .NET Console

Monday, June 6, 2005

OK, with support for user functions in .NET Console, and plenty of dynamic event management utilities already available, it's no surprise that .NET Console supports hooking up user function to events:

handleIt(sender, e){WebEditApp.Trace(string.Concat("active doc changed: ", e.Document));}
Gregor.NetConsole.UserHandler.Add(WebEditApp.MainForm, "ActiveDocumentChanged", handleIt)

There's no language support yet for event manipulation statements, thus the helper routine in Gregor.Core.UserHandler. Note that the event must follow the usual pattern (with some parameter contravariance allowed), and the user function that handles the event must have a couple of parameters.

Handling events this way is the only instance of having .NET code call back into user code in .NET Console. I wouldn't know how to simulate virtual function overrides or interface member implementation without compiling to IL (intermediate language). Events can be handled only because there are strong coding conventions governing their signature; so one real (ie., compiled) method can be used as the handler for any event on any sender. The call can be forwarded to the appropriate user function in the interpreter, since the handler is an instance method (see Gregor.Core.CHandler and Gregor.NetConsole.CUserHandler for the implementation).

The CHandler class is an old acquaintance: we've met it before as a universal event handler used in declarative event handling in WebEdit.NET Script AddIns.