Note that there are some explanatory texts on larger screens.

plurals
  1. POC# 2.0 Dynamic Event Subscription with known event signature
    text
    copied!<p>Similarly to this question:</p> <p><a href="https://stackoverflow.com/questions/45779/c-dynamic-event-subscription">C# Dynamic Event Subscription</a></p> <p>I would like to be able to wire up an event handler to an event fired out of a dynamically-created object. I am doing this to verify that my JavaScript and other non-.NET code is able to connect to the objects' events.</p> <p>My event signature in the object is:</p> <p>delegate void MediaItemFellbackDelegate(int id, string name, string path);</p> <p>Here is my 'DynamicHost' code:</p> <pre><code>public delegate void MediaItemFellbackDelegate(int id, string name, string path); public void MediaItemFellback(int id, string name, string path) { } private void HandleEvent(string eventName, Delegate handler) { try { BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; EventInfo mediaItemFellback = m_PlayerType.GetEvent(eventName, bindingFlags); mediaItemFellback.AddEventHandler(m_Player, handler); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } HandleEvent("MediaItemFellback", new MediaItemFellbackDelegate(this.MediaItemFellback)); </code></pre> <p>but I end up with the exception:</p> <blockquote> <p>Object of type 'DynamicHost.Main+MediaItemFellbackDelegate' cannot be converted to type 'Player.MediaItemFellbackDelegate'.</p> </blockquote> <p>in the AddEventHandler() call.</p> <p>Of course, they are different types, but the signatures are identical.</p> <p>What's going wrong here?</p>
 

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