public void Intercept(IInvocation invocation) { object newProxy; MethodInfo proxyMethod; var aopInterceptors = GetMethodInterceptors(invocation.MethodInvocationTarget); var remainingInterceptors = GetRemainingInterceptors(invocation.Proxy); var mergeInterceptors = aopInterceptors.Union(remainingInterceptors); newProxy = InterfaceProxyFactory.CreateInterfaceProxy(invocation.InvocationTarget, mergeInterceptors.ToArray()); proxyMethod = invocation.Method; // same interface method to the new proxy try { invocation.ReturnValue = proxyMethod.Invoke(newProxy, invocation.Arguments); } catch (TargetInvocationException ex) { if (ex.InnerException != null) { throw ex.InnerException; } throw ex; } }
/********* ** Public methods *********/ /// <summary>Construct an instance.</summary> /// <param name="modID">The unique ID of the relevant mod.</param> /// <param name="registry">The underlying mod registry.</param> /// <param name="proxyFactory">Generates proxy classes to access mod APIs through an arbitrary interface.</param> /// <param name="monitor">Encapsulates monitoring and logging for the mod.</param> public ModRegistryHelper(string modID, ModRegistry registry, InterfaceProxyFactory proxyFactory, IMonitor monitor) : base(modID) { this.Registry = registry; this.ProxyFactory = proxyFactory; this.Monitor = monitor; }
public override void PostBuildUp(ref BuilderContext context) { if (context.Existing == null) { base.PostBuildUp(ref context); return; } var container = context.Container; var interceptors = new List <IInterceptor>(); var types = GetUnproxiedTypes(context.Existing); // class interceptor var typeInterceptorsMap = AopInterceptorContainer.FindInterceptors(AttributeTargets.Class | AttributeTargets.Interface); foreach (var type in types) { foreach (var attribute in type.GetCustomAttributes(false)) { if (typeInterceptorsMap.ContainsKey(attribute.GetType())) { interceptors.AddRange(typeInterceptorsMap[attribute.GetType()].Select(t => (IInterceptor)container.Resolve(t))); } } } // method interceptor if (IsMethodBootstrapInterceptorNeeded(types)) { interceptors.Add(container.Resolve <AopMethodBootstrapInterceptor>()); } if (interceptors.Count() > 0) { if (ProxyUtil.IsProxy(context.Existing)) { AddInterceptorsToProxy(context.Existing, interceptors); } else { context.Existing = InterfaceProxyFactory.CreateInterfaceProxy(context.Existing, interceptors.ToArray()); } } if (ProxyUtil.IsProxy(context.Existing) && !HasInterceptors(context.Existing)) { throw new InvalidOperationException($"No interceptor found for this proxy bean {context.Existing}."); } base.PostBuildUp(ref context); }