Search code examples
castle-windsor

Castle Windsor Interceptors - get Target already in the ctor


When I'm using interceptors in Castle Windsor, I can get access to the invocation target whenever a function is called (invocation parameter of type IInvocation).

Is it possible to get the target of the interceptor already in the constructor of this interceptor, so that I can write code like follows:

public class MyInterceptor: IInterceptor
{
   public MyInterceptor(ITargetOfInterception target, ILogger logger) 
   { 
       logger.Log("Interceptor created for target type: " + target.GetType() );
   }
}

Solution

  • It's not possible to get access to the target in the constructor but you can achieve what you're after by implementing IOnBehalfAware (see doco here)

    public void SetInterceptedComponentModel(ComponentModel target)
    {
        logger.Log("Interceptor created for target: " + target.ToString());
    }