How do I tell Windsor to add an Interceptor to all components registered that implement IMustBeIntercepted

前端 未结 2 1975
感情败类
感情败类 2021-01-03 01:47

If I registered several components with Windsor.

IAnimal provides BigAnimal IPerson provides SmellyPerson IWhale provides BlueWhale

etc.. pretty standard com

2条回答
  •  暖寄归人
    2021-01-03 02:35

    Simply create an interface like this:

    public interface IMustBeIntercepted {}
    

    and a facility like this:

    public class InterceptionFacility : AbstractFacility {
        protected override void Init() {
            Kernel.ComponentRegistered += new Castle.MicroKernel.ComponentDataDelegate(Kernel_ComponentRegistered);
        }
    
        void Kernel_ComponentRegistered(string key, Castle.MicroKernel.IHandler handler) {
            if(typeof(IMustBeIntercepted).IsAssignableFrom(handler.ComponentModel.Implementation)) {
                handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(TestInterceptor)));
            }
        }
    }
    

    Then register the facility to the container using the tag. Now all components that implements IMustBeIntercepted will be intercepted by the interceptor TestInterceptor.

提交回复
热议问题