Similar to this question:
C # dynamic event subscription
I would like to be able to attach an event handler to an event fired from a dynamically created object. I do this to make sure that my JavaScript and other non-.NET code cannot connect to object events.
My event signature in the object:
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 get an exception:
An object of type 'DynamicHost.Main + MediaItemFellbackDelegate' cannot be converted to type 'Player.MediaItemFellbackDelegate'.
in the call to AddEventHandler ().
Of course, these are different types, but the signatures are identical.
What is wrong here?