Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing reflection to specify the type of a delegate (to attach to an event)?
    primarykey
    data
    text
    <p>What I effectively want to do is something like this (I realise this is not valid code):</p> <pre><code>// Attach the event. try { EventInfo e = mappings[name]; (e.EventHandlerType) handler = (sender, raw) =&gt; { AutoWrapEventArgs args = raw as AutoWrapEventArgs; func.Call(this, args.GetParameters()); }; e.AddEventHandler(this, handler); } ... </code></pre> <p>Now I know that the e.EventHandlerType will always <strong>derive</strong> from EventHandler&lt;AutoWrapEventArgs&gt;. However, I can't just do:</p> <pre><code> EventHandler&lt;AutoWrapEventArgs&gt; handler = (sender, raw) =&gt; { AutoWrapEventArgs args = raw as AutoWrapEventArgs; func.Call(this, args.GetParameters()); }; e.AddEventHandler(this, handler); </code></pre> <p>As .NET complains that there is no conversion applicable from EventHandler&lt;AutoWrapEventArgs&gt; to EventHandler&lt;DataEventArgs&gt; when AddEventHandler is called. This is the exact message:</p> <pre><code>Object of type 'System.EventHandler`1[IronJS.AutoWrapObject+AutoWrapEventArgs]' cannot be converted to type 'System.EventHandler`1[Node.net.Modules.Streams.NodeStream+DataEventArgs]'. </code></pre> <p>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).</p> <p>Is there a way I can use <a href="https://en.wikipedia.org/wiki/Reflection_%28computer_programming%29" rel="nofollow">reflection</a> to get around this problem?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload