This is what I came up with:
_container.RegisterInterceptor<IPersonManager, LoggingInterceptor>();
-
public static class ContainerExtensions
{
public static void RegisterInterceptor<TDependency, TInterceptor>(this IContainer container)
where TDependency : class
where TInterceptor : IInterceptor
{
IInterceptor interceptor = container.GetInstance<TInterceptor>();
if (interceptor == null)
throw new NullReferenceException("interceptor");
TypeInterceptor typeInterceptor
= new GenericTypeInterceptor<TDependency>(interceptor);
container.Configure(c => c.RegisterInterceptor(typeInterceptor));
}
}
-
public class GenericTypeInterceptor<TDependency> : TypeInterceptor
where TDependency : class
{
private readonly IInterceptor _interceptor;
private readonly ProxyGenerator _proxyGenerator = new ProxyGenerator();
public GenericTypeInterceptor(IInterceptor interceptor)
{
if (interceptor == null)
throw new ArgumentNullException("interceptor");
_interceptor = interceptor;
}
public object Process(object target, IContext context)
{
return _proxyGenerator.CreateInterfaceProxyWithTarget(target as TDependency, _interceptor);
}
public bool MatchesType(Type type)
{
return typeof(TDependency).IsAssignableFrom(type);
}
}
I'm pretty happy with the result.