Open tillig opened 5 years ago
Just for clarity and searchability, the sample code from the question which uses InterceptorSelector
from the Castle library:
class Program
{
static void Main(string[] args)
{
var builder = new ContainerBuilder();
var proxyGenerationOptions = new ProxyGenerationOptions();
//I want to use this
//proxyGenerationOptions.Selector = new InterceptorSelector();
builder.RegisterType<SomeType>()
.As<ISomeInterface>()
.EnableInterfaceInterceptors(proxyGenerationOptions)
.InterceptedBy(typeof(CallLogger));//and remove explicit statement
builder.Register<TextWriter>(x => Console.Out);
builder.RegisterType<CallLogger>().AsSelf();
var container = builder.Build();
var willBeIntercepted = container.Resolve<ISomeInterface>();
willBeIntercepted.Work();
}
}
public class InterceptorSelector : IInterceptorSelector
{
public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IInterceptor[] interceptors)
{
//don't know how to solve dependency here, because it's registration time
return new IInterceptor[] { /* new CallLogger(dependency) or InterceptorReference.ForType<CallLogger>() */};
}
}
public class CallLogger : IInterceptor
{
TextWriter _output;
public CallLogger(TextWriter output)
{
_output = output;
}
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
_output.WriteLine("Done: result was {0}.", invocation.ReturnValue);
}
}
public interface ISomeInterface { void Work(); }
public class SomeType : ISomeInterface { public void Work() { } }
Based on this StackOverflow question it may be interesting to look at allowing the proxy generation options
Selector
influence the choice for interceptors.