What I really want to do is something like this (I understand that this is invalid code):
// Attach the event. try { EventInfo e = mappings[name]; (e.EventHandlerType) handler = (sender, raw) => { AutoWrapEventArgs args = raw as AutoWrapEventArgs; func.Call(this, args.GetParameters()); }; e.AddEventHandler(this, handler); } ...
Now I know that e.EventHandlerType will always output from EventHandler <AutoWrapEventArgs>. However, I cannot just do:
EventHandler<AutoWrapEventArgs> handler = (sender, raw) => { AutoWrapEventArgs args = raw as AutoWrapEventArgs; func.Call(this, args.GetParameters()); }; e.AddEventHandler(this, handler);
Because .NET complains that there is no conversion applicable in EventHandler <AutoWrapEventArgs> to EventHandler <DataEventArgs> when AddEventHandler is called. This is the exact message:
Object of type 'System.EventHandler`1[IronJS.AutoWrapObject+AutoWrapEventArgs]' cannot be converted to type 'System.EventHandler`1[Node.net.Modules.Streams.NodeStream+DataEventArgs]'.
I also tried using Invoke to dynamically use the e.EventHandlerType constructor, but there is no way to pass the delegate definition to the Invoke () parameter list (because there is no conversion from delegate to object).
Can reflection be used to get around this?
June Rhodes
source share