//====================================================================
// Register a new Python object event handler with the event.
//====================================================================
internal bool AddEventHandler(IntPtr target, IntPtr handler)
{
Object obj = null;
if (target != IntPtr.Zero)
{
CLRObject co = (CLRObject)ManagedType.GetManagedObject(target);
obj = co.inst;
}
// Create a true delegate instance of the appropriate type to
// wrap the Python handler. Note that wrapper delegate creation
// always succeeds, though calling the wrapper may fail.
Type type = this.info.EventHandlerType;
Delegate d = DelegateManager.GetDelegate(type, handler);
// Now register the handler in a mapping from instance to pairs
// of (handler hash, delegate) so we can lookup to remove later.
// All this is done lazily to avoid overhead until an event is
// actually subscribed to by a Python event handler.
if (reg == null)
{
reg = new Hashtable();
}
object key = (obj != null) ? obj : this.info.ReflectedType;
ArrayList list = reg[key] as ArrayList;
if (list == null)
{
list = new ArrayList();
reg[key] = list;
}
list.Add(new Handler(Runtime.PyObject_Hash(handler), d));
// Note that AddEventHandler helper only works for public events,
// so we have to get the underlying add method explicitly.
object[] args = { d };
MethodInfo mi = this.info.GetAddMethod(true);
mi.Invoke(obj, BindingFlags.Default, null, args, null);
return(true);
}