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 NotifierEventBinder(object notifier, EventInfo info, Action <object> invoker) { _Info = info; _Instance = notifier; _Invoker = invoker; Type type = info.DeclaringType.GetGenericArguments()[0]; Utility.Reflection.TypeMethodCatcher catcherSupply = new Regulus.Utility.Reflection.TypeMethodCatcher((System.Linq.Expressions.Expression <Action <NotifierEventBinder> >)(ins => ins._Supply <object>(null))); MethodInfo supplyGenericMethod = catcherSupply.Method.GetGenericMethodDefinition(); MethodInfo supplyMethod = supplyGenericMethod.MakeGenericMethod(type); Type actionT1 = typeof(System.Action <>); Type actionT = actionT1.MakeGenericType(type); Delegate delegateSupply = Delegate.CreateDelegate(actionT, this, supplyMethod); _Delegate = delegateSupply; }