public AgentEventRectifier(IEnumerable <Type> types, INotifierQueryable agent_instance) { this._Types = types.ToArray(); _RemoveHandlers = new List <System.Action>(); Type agentType = typeof(INotifierQueryable); System.Reflection.MethodInfo agentQueryNotifier = agentType.GetMethod(nameof(INotifierQueryable.QueryNotifier)); foreach (Type type in _Types) { if (!type.IsInterface) { continue; } System.Reflection.MethodInfo agentQueryNotifierT = agentQueryNotifier.MakeGenericMethod(type); object notifyInstance = agentQueryNotifierT.Invoke(agent_instance, new object[0]); Type notifyTypeT = typeof(INotifier <>).MakeGenericType(type); System.Reflection.EventInfo notifySupply = notifyTypeT.GetEvent(nameof(INotifier <object> .Supply)); System.Reflection.EventInfo notifyUnsupply = notifyTypeT.GetEvent(nameof(INotifier <object> .Unsupply)); Utility.Reflection.TypeMethodCatcher catcherSupply = new Regulus.Utility.Reflection.TypeMethodCatcher((System.Linq.Expressions.Expression <Action <AgentEventRectifier> >)(ins => ins._Supply <object>(null))); System.Reflection.MethodInfo supplyGenericMethod = catcherSupply.Method.GetGenericMethodDefinition(); System.Reflection.MethodInfo supplyMethod = supplyGenericMethod.MakeGenericMethod(type); Utility.Reflection.TypeMethodCatcher catcherUnsupply = new Regulus.Utility.Reflection.TypeMethodCatcher((System.Linq.Expressions.Expression <Action <AgentEventRectifier> >)(ins => ins._Unsupply <object>(null))); System.Reflection.MethodInfo unsupplyGenericMethod = catcherUnsupply.Method.GetGenericMethodDefinition(); System.Reflection.MethodInfo unsupplyMethod = unsupplyGenericMethod.MakeGenericMethod(type); Type actionT1 = typeof(System.Action <>); Type actionT = actionT1.MakeGenericType(type); Delegate delegateSupply = Delegate.CreateDelegate(actionT, this, supplyMethod); Delegate delegateUnsupply = Delegate.CreateDelegate(actionT, this, unsupplyMethod); notifySupply.AddEventHandler(notifyInstance, delegateSupply); notifyUnsupply.AddEventHandler(notifyInstance, delegateUnsupply); _RemoveHandlers.Add(() => { notifySupply.RemoveEventHandler(notifyInstance, delegateSupply); notifyUnsupply.RemoveEventHandler(notifyInstance, delegateUnsupply); }); } }
public override void RemoveEventHandler(object target, Delegate handler) { if (Marshal.IsComObject(target)) { // retrieve sourceIid and dispid Guid sourceIid; int dispid; GetDataForComInvocation(_innerEventInfo, out sourceIid, out dispid); System.Runtime.InteropServices.ComEventsHelper.Remove(target, sourceIid, dispid, handler); } else { // we are dealing with a managed object - just add the delegate through reflection _innerEventInfo.RemoveEventHandler(target, handler); } }
/// <summary> /// Removes the event handler. /// </summary> virtual protected void RemoveHandler() { // We can't use RemoveEventHandler on AOT based platforms #if UNITY_IPHONE || UNITY_XBOX360 || UNITY_PS3 || UNITY_PSP2 || UNITY_XBOXONE || UNITY_PS4 || UNITY_WIIU || UNITY_WEBGL if (eventInfo != null && handler != null && sendingComponent != null) { System.Reflection.MethodInfo handleMethod = this.GetType().GetMethod("HandleEvent", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); handler = System.Delegate.CreateDelegate(eventInfo.EventHandlerType, this, handleMethod); System.Reflection.MethodInfo removeMethod = eventInfo.GetRemoveMethod(); removeMethod.Invoke(sendingComponent, new object[] { handler }); } #else // Remove listeners if (eventInfo != null && handler != null && sendingComponent != null) { eventInfo.RemoveEventHandler(sendingComponent, handler); } #endif }
public override void RemoveEventHandler(object target, Delegate handler) { if (Marshal.IsComObject(target)) { // retrieve sourceIid and dispid Guid sourceIid; int dispid; GetDataForComInvocation(_innerEventInfo, out sourceIid, out dispid); // now validate the caller can call into native and redirect to ComEventHelpers.Combine SecurityPermission perm = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode); perm.Demand(); System.Runtime.InteropServices.ComEventsHelper.Remove(target, sourceIid, dispid, handler); } else { // we are dealing with a managed object - just add the delegate through relection _innerEventInfo.RemoveEventHandler(target, handler); } }
private void detachEventHandler(System.Reflection.EventInfo @event, Delegate d, object realButton) { @event.RemoveEventHandler(realButton, d); }