Example #1
0
 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);
     }
 }
Example #2
0
 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();
 }
Example #3
0
        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;
        }