這裏有一個辦法做到這一點,利用IHandlerSelector
:
public class DebugHandlerSelector: IHandlerSelector {
private readonly Type serviceType;
private readonly Type debugImplementation;
private readonly Type releaseImplementation;
public DebugHandlerSelector(Type serviceType, Type debugImplementation, Type releaseImplementation) {
this.serviceType = serviceType;
this.debugImplementation = debugImplementation;
this.releaseImplementation = releaseImplementation;
}
public bool HasOpinionAbout(string key, Type service) {
return service == serviceType;
}
public IHandler SelectHandler(string key, Type service, IHandler[] handlers) {
return handlers.First(h => h.ComponentModel.Implementation ==
#if DEBUG
debugImplementation
#else
releaseImplementation
#endif
);
}
}
使用範例:
container.Kernel.AddHandlerSelector(new DebugHandlerSelector(typeof(IMethodService), typeof(MethodAService), typeof(MethodBService)));
問題補充到溫莎FAQ:http://using.castleproject.org/display/IoC/FAQ – 2010-01-24 17:02:35