protected static void CreateClassProxy(IInterceptor[] interceptors, ActivatingEventArgs <object> e, Type type, object instance, Type[] proxiedInterfaces) { // Ensure we don't try to proxy anything that can't be proxied if (type.IsSealed) { return; } var targetType = type; // Special handling for nested proxies... if (ProxyUtil.IsProxy(instance)) { OverrideProxyInterceptors(interceptors, instance); } else { var proxyType = ProxyGenerator.ProxyBuilder.CreateClassProxyType(targetType, proxiedInterfaces, ProxyGenerationOptions.Default); var activator = new ReflectionActivator(proxyType, new DefaultConstructorFinder(), new MostParametersConstructorSelector(), GetConfiguredParams(ProxyGenerationOptions.Default, interceptors), Enumerable.Empty <Parameter>()); e.ReplaceInstance(activator.ActivateInstance(e.Context, e.Parameters)); // Dispose of the old instance, if necessary var oldInstance = instance as IDisposable; oldInstance?.Dispose(); } }
private void Registration_Activating(Object sender, ActivatingEventArgs <Object> e) { Object proxy = this._generator.CreateClassProxyWithTarget( e.Instance.GetType(), e.Instance, e.Context.Resolve <IEnumerable <IInterceptor> >().ToArray()); e.ReplaceInstance(proxy); }
private void OnComponentActivating(object sender, ActivatingEventArgs <object> e) { var originalInstance = e.Instance; var newInstance = _interceptor.OnResolving(originalInstance, e.Context); if (newInstance != originalInstance) { e.ReplaceInstance(newInstance); } }
protected static void CreateInterfaceProxy(IInterceptor[] interceptors, ActivatingEventArgs <object> e, Type[] proxiedInterfaces, object instance) { // Ensure we don't proxy anything from Castle, to avoid proxying known-unknown proxies if (!proxiedInterfaces.Any()) { return; } // intercept with all interfaces var theInterface = proxiedInterfaces.First(); var interfaces = proxiedInterfaces.Skip(1).ToArray(); // Special handling for nested proxies... if (ProxyUtil.IsProxy(instance)) { OverrideProxyInterceptors(interceptors, instance); } else { e.ReplaceInstance(ProxyGenerator.CreateInterfaceProxyWithTarget(theInterface, interfaces, instance, interceptors)); } }