This project has moved and is read-only. For the latest updates, please go here.

AddEventHandller implementation

Aug 3, 2014 at 11:55 AM
hi,
i would implement my own AddEventHandler.
my code:
public class Window
    {
        private Form f;
        public Window(Form f)
        {
            this.f = f;
        }

        public void AddEventHandler(string name, dynamic handler)
        {
            f.GetType().GetEvent(name).AddEventHandler(f, Delegate.CreateDelegate(f.GetType(), new Action<string,string>((s, s1) => handler(s, s1)).Method));
        }
    }
but it throws an exception: The type must be inferred from the delegate.
Parameter name: type
Coordinator
Aug 4, 2014 at 3:15 AM
Hi furesoft,

It looks like you might want something like this:
public void AddEventHandler(string name, dynamic handler)
{
    var evt = f.GetType().GetEvent(name);
    var action = new Action<object, object>((sender, args) => handler(sender, args));
    evt.AddEventHandler(f, Delegate.CreateDelegate(evt.EventHandlerType, action.Target, action.Method));
}
Keep in mind that ClearScript lets you set up event handlers directly from script code:
form.Activated.connect(function (sender, args) {
    // do something
});
Good luck!
Marked as answer by furesoft on 8/4/2014 at 4:34 AM