What I effectively want to do is something like this (I realise this is not valid 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 the e.EventHandlerType will always derive from EventHandler<AutoWrapEventArgs>. However, I can't just do:
EventHandler<AutoWrapEventArgs> handler = (sender, raw) =>
{
AutoWrapEventArgs args = raw as AutoWrapEventArgs;
func.Call(this, args.GetParameters());
};
e.AddEventHandler(this, handler);
As .NET complains that there is no conversion applicable from 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 have also tried using Invoke to dynamically use the constructor of e.EventHandlerType, but there's no way to pass the delegate definition to Invoke()'s parameter list (because there is no conversion from delegate to object).
Is there a way I can use reflection to get around this problem?