Invoke Castle Interceptor on Component Instance

214 Views Asked by At

Is there any easy way of getting a method on my IInterceptor to get called as soon as any instance for the relevant component is resolved? Sort of like IOnBehalfAware, but so that it gets called with the actual component instance, not the ComponentModel.

1

There are 1 best solutions below

0
On BEST ANSWER

Ended up doing something like this...define an interface that my IInterceptor(s) implement:

public interface IInstanceAware
{
   void Execute(object instance); 
}

Then when registering component(s), do

registration.OnCreate((kernel, instance) => 
{
   var accessor = instance as IProxyTargetAccessor;
   foreach(var instanceAware in accessor.GetInterceptors().OfType<IInstanceAware>())
   { 
       accessor.Execute(instance);
   }
};