Can .Net events be tied to JS functions?

Jul 28, 2011 at 2:16 PM
Edited Jul 28, 2011 at 2:20 PM

I looked at the Microsoft docs on their defunct JScript.net and this was one of its weaknesses. JScript can consume .Net events but it can't create the delegate objects necessary to assign JS code as callable functions. For example, you could create a Windows Form button on the JS side, but you couldn't attach a JS function to the Click event because there's no way to create the required delegate.

Given that Paul is the master of the Jurassic domain, I'm assuming that this is possible...if he wants it to be.

Would some facility like this be a matter of creating a general purpose Delegate object which handles the binding? I've seen this type of thing before with a charting package called eSignal. They use JS for their scripting and have some custom object types to do things like bind C DLLs calls to the JS level, a DDE (Dynamic Data Exchange) object, a File object for common text file manipulation, etc. [eSignal isn't a .Net app so they had to come up with these custom JS objects for extensions to the JS core they're using (Spidermonkey)].

[Of course, I'm punting on the implementation because I've got app-level integration plans for Jurassic first and I can get away with keeping delegate creation on the C# side].

Jul 28, 2011 at 2:30 PM

Well, I'm not too sure about events, but what I do know is that calling a js-function from .NET code, and calling a .NET-function from js-code is possible. Thus, events should be too. You could just create a function that consumes a js-function and returns a delegate (if js-functions isn't already delegates), and then you'd have a delegate-function you could assign as your eventhandler. If all this is mappable from javascript, I don't know, but it should (in theory) be possible to create.

Coordinator
Aug 4, 2011 at 8:16 AM

It's completely possible to do.  The main difficulty is creating a wrapper function that mimics the signature of the event.  This is not too hard and in fact most of the code to do that is already in the nightly build.  There is an additional difficulty that you need some way to add or remove from the event - but if I remember correctly events are actually just a pair of methods exposed on a class (add_EventName and remove_EventName) so you could follow that convention without too much trouble.