<PackageReference Include="Castle.Windsor" Version="5.0.0" />

AbstractProxyFactory

public abstract class AbstractProxyFactory : IProxyFactory
using Castle.Core; using Castle.Core.Interceptor; using Castle.DynamicProxy; using Castle.MicroKernel; using Castle.MicroKernel.Context; using Castle.MicroKernel.Proxy; using Castle.MicroKernel.Resolvers; using System; using System.Collections.Generic; using System.Linq; namespace Castle.Windsor.Proxy { public abstract class AbstractProxyFactory : IProxyFactory { private List<IModelInterceptorsSelector> selectors; public abstract object Create(IKernel kernel, object instance, ComponentModel model, CreationContext context, params object[] constructorArguments); public abstract object Create(IProxyFactoryExtension customFactory, IKernel kernel, ComponentModel model, CreationContext context, params object[] constructorArguments); public abstract bool RequiresTargetInstance(IKernel kernel, ComponentModel model); public void AddInterceptorSelector(IModelInterceptorsSelector selector) { if (selectors == null) selectors = new List<IModelInterceptorsSelector>(); selectors.Add(selector); } public bool ShouldCreateProxy(ComponentModel model) { if (model.HasInterceptors) return true; ProxyOptions proxyOptions = model.ObtainProxyOptions(false); if (proxyOptions != null && proxyOptions.RequiresProxy) return true; if (selectors != null && selectors.Any((IModelInterceptorsSelector s) => s.HasInterceptors(model))) return true; return false; } protected IEnumerable<InterceptorReference> GetInterceptorsFor(ComponentModel model) { InterceptorReference[] array = model.Interceptors.ToArray(); if (selectors != null) { foreach (IModelInterceptorsSelector selector in selectors) { if (selector.HasInterceptors(model)) { array = selector.SelectInterceptors(model, array); if (array == null) array = new InterceptorReference[0]; } } return array; } return array; } protected IInterceptor[] ObtainInterceptors(IKernel kernel, ComponentModel model, CreationContext context) { List<IInterceptor> list = new List<IInterceptor>(); foreach (InterceptorReference item in GetInterceptorsFor(model)) { try { IInterceptor val = ((IReference<IInterceptor>)item).Resolve(kernel, context); SetOnBehalfAware(val as IOnBehalfAware, model); list.Add(val); } catch (Exception ex) { foreach (IInterceptor item2 in list) { kernel.ReleaseComponent(item2); } if (ex is InvalidCastException) throw new DependencyResolverException($"""{model.Name}""{typeof(IInterceptor).get_Name()}"""); throw; } } return list.ToArray(); } protected static void SetOnBehalfAware(IOnBehalfAware onBehalfAware, ComponentModel target) { onBehalfAware?.SetInterceptedComponentModel(target); } } }