-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for adding/removing event handlers to/from events without any managed utils #23
Conversation
This looks very interesting - the original event handler code was implemented long before we had extended the binding scope of the .NET base class libraries. I would love to get the code generator to output event binding : it would be very cool. The manual approach works but it's pretty laborious to set up. I need to leave myself an hour to go through this though. It would be good if you could add something to the unit test code for this as there is a lot going on in Dubrovnik and the tests are crucial for maintaining a degree of confidence in all this. It's great to have your input. |
Sure, take your time to review! I can't claim to be 100% sure this works in all cases though and you're right, unit tests are definitely required if you were to merge this into master. I think it would make more sense for you to review the code and evaluate if the approach I took makes sense from your perspective. If that's the case, we can work together on providing tests, changing the implementation if required and upgrading the samples to use the new event handling stuff. And yes, getting the code generator to output event binding code would be tremendously helpful! |
I agree. I will get back to you on this as soon as I can. |
I played with this a bit more and added custom bindings that hide most of the event handler plumbing and makes strongly typed event handlers possible. Would be really awesome if the generator could create something like this automatically. Consider the following managed event:
I now have the following manual native bindings:
Now, in Swift I can do something like this:
Alternatively, you can just handle the event in a separate function:
|
I should get time to look at this at the weekend. Do you have to update the pull request? I use Dubrovnik exclusively from Obj-C. Did you have many issues using Swift? Have you configured module map files? |
We've been using it from Swift (with a self-created module map and that works ok (might be nicer if Mono was modularized though) - actually - see #15 that I opened ages back and forgot about). We've also been revisiting the Sequence thing I mentioned in #12 - works more nicely when the bindings for the collection classes don't have holes where their IEnumerable support should be :-) |
No need to update the PR, I've been working off master + the changes from this PR the last couple days. Regarding Swift: I'm using Swift only in the Cocoa App project, not in the framework libraries. Didn't have to deal with module maps. Instead I just made sure my prefix header is imported in the Swift header. This gives me access to everything I need. No specific problems at the moment with this approach, although obviously the method names are not very Swifty but I can live with that. ;) |
Is that still an issue now that we have better mscorlib bindings? |
I think the actual binding bug is fixed now. |
I didn't merge this directly but I have tried to include the functionality. I moved some of the methods into a System_Object category rather than include them in System_Object directly. This didn't look right
So I modified it to I haven't been able to trial this out as yet but I should get opportunity soon. |
…implicit tests for this new functionality.
Perfect! Thx! |
Like the title says, this PR allows you to add and remove event handlers to/from events without any managed helper classes. It builds upon the existing support for creating
System.Delegate
objects by specifying a block.The implementation makes use of the reflection APIs to call
AddEventHandler
andRemoveEventHandler
on the event you specify.Here's an example:
Given you have an event declared in C# like this:
... you can now do the following in Dubrovnik to subscribe to the event:
All event handlers registered with this mechanism are tracked and kept in memory until either
removeEventHandler:fromEventNamed:
is called or the object itself is deallocated.There's also a new method for retrieving all currently registered native(!) event handlers for a specific event called
eventHandlersForEventNamed:
.I guess with this in place it would be quite easy to extend the code generation utils to actually support generating bindings for events as well but I haven't (yet) looked at this.
What do you think?