public IPipeline RegisterObserver(IPipelineObserver pipelineObserver) { Observers.Add(pipelineObserver); var observerInterfaces = pipelineObserver.GetType().GetInterfaces(); var implementedEvents = from i in observerInterfaces where i.IsAssignableTo(typeof(IPipelineObserver <>)) && i.IsGenericType && i.Name.StartsWith("IPipelineObserver`") select i; foreach (var @event in implementedEvents) { var pipelineEventType = @event.GetGenericArguments()[0]; if (!ObservedEvents.TryGetValue(pipelineEventType, out _)) { ObservedEvents.Add(pipelineEventType, new List <ObserverMethodInfoPair>()); } var methodInfo = pipelineObserver.GetType().GetMethod("Execute", new[] { pipelineEventType }); ObservedEvents[pipelineEventType].Add(new ObserverMethodInfoPair(pipelineObserver, methodInfo)); } return(this); }
public ObserverMethodInvoker(IPipelineObserver pipelineObserver, Type pipelineEventType) { _pipelineObserver = pipelineObserver; var pipelineObserverType = pipelineObserver.GetType(); var dynamicMethod = new DynamicMethod(string.Empty, typeof(void), new[] { typeof(IPipelineObserver), typeof(IPipelineEvent) }, typeof(IPipelineEvent).Module); var il = dynamicMethod.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_1); var methodInfo = pipelineObserverType.GetMethod("Execute", new[] { pipelineEventType }); il.EmitCall(OpCodes.Callvirt, methodInfo, null); il.Emit(OpCodes.Ret); _invoker = (InvokeHandler)dynamicMethod.CreateDelegate(typeof(InvokeHandler)); }
public string GetObserverTypeName() => _pipelineObserver.GetType().FullName;