public void Add(MethodInfo methodInfo, IGeneralInterceptor interceptor) { if (interceptorsMap.TryGetValue(methodInfo, out IGeneralInterceptor currentInterceptor)) { interceptorsMap[methodInfo] = new CompositeInterceptor(currentInterceptor, interceptor); } else { interceptorsMap.Add(methodInfo, interceptor); } }
public CompositeInterceptor(IGeneralInterceptor previous, params IGeneralInterceptor[] newInterceptors) { interceptors = (previous is CompositeInterceptor compositeInterceptor ? compositeInterceptor.interceptors.Concat(newInterceptors) : new[] { previous }.Concat(newInterceptors)). ToArray(); methods = interceptors .Select(interceptor => interceptor.MainDefinition) .OfType <Method>() .ToArray(); }
public void Intercept(IInvocation invocation) { // find local instance from instanceMap MethodInfo methodInfo = invocation.Method; IGeneralInterceptor generalInterceptor = interceptorsMap.Find(methodInfo); if (generalInterceptor == null) { throw new MissingMethodException($"{methodInfo} was not implemented"); } object value = methodInfo.GetDefaultReturnValue(); generalInterceptor .Invoke( instanceMap, ref value, invocation.Arguments, invocation.GenericArguments, methodInfo); invocation.ReturnValue = value; }