在这种情况下,我的应用程序收到了一个已经初始化UnityContainer
的已注册类型,归结为:
container.RegisterType<IService>(new InjectionFactory(c => new Service()));
我需要实现的是ServiceInterceptor
在IService
注册中添加一个拦截器。我想显而易见的答案是:通过运行第二个RegisterType<IService>
并将拦截器应用为注入成员来做到这一点。然而,不幸的是,如下所述重新创建提供的注入工厂和委托是不可行的。new Service()
我目前无法获得该声明。
container.RegisterType<IService>(
new InjectionFactory(c => new Service()),
new Interceptor<InterfaceInterceptor>(),
new InterceptionBehavior<ServiceInterceptor>());
所以:我正在寻找一种方法来将更多的注入成员添加到现有的ContainerRegistration
.
// 1. Get the current container registration
var containerRegistration = container.Registrations
.First(cr => cr.RegisteredType == typeof(IService));
// 2. Is this even possible?
ApplyInterception(
containerRegistration,
new Interceptor<InterfaceInterceptor>(),
new InterceptionBehavior<ServiceInterceptor>());
// 3. Profit!