views:

193

answers:

1

Hi,

Similarly to this question:

http://stackoverflow.com/questions/45779/c-dynamic-event-subscription

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.

My event signature in the object is:

delegate void MediaItemFellbackDelegate(int id, string name, string path);

Here is my 'DynamicHost' 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));

but I end up with the exception:

Object of type 'DynamicHost.Main+MediaItemFellbackDelegate' cannot be converted to type 'Player.MediaItemFellbackDelegate'.

in the AddEventHandler() call.

Of course, they are different types, but the signatures are identical.

What's going wrong here?

+2  A: 

Quite simply, you can't subscribe to an event using the wrong kind of delegate.

What you could do is create the right kind of delegate in HandleEvent:

private void HandleEvent(string eventName, Delegate handler)
{
  try
  {
    BindingFlags bindingFlags = BindingFlags.Instance | 
                                BindingFlags.Public | BindingFlags.NonPublic;
    EventInfo mediaItemFellback = m_PlayerType.GetEvent(eventName, bindingFlags);
    Delegate correctHandler = Delegate.CreateDelegate(
        mediaItemFellback.EventHandlerType, handler.Target, handler.Method);
    mediaItemFellback.AddEventHandler(m_Player, correctHandler);
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.ToString());
  }
}
Jon Skeet
You rock! All the examples I could find descend into IL generation, so thanks for your answer. You rock!
JBRWilkinson