我会提出一个我认为可行的解决方案。默认情况下,CM 分配IoC.GetInstance()
和所有其他Func<>
代表BootstrapperBase
就像这样:
IoC.GetInstance = this.GetInstance
Where this.GetInstance
is只是一个BootstrapperBase
虚拟且空的方法,因此您可以在自己的派生引导程序中覆盖它。
我尝试的解决方案:存储对已注册内容的引用IoC.GetInstance
并在您的新GetInstance
覆盖中调用它,并对其他两个 staticFunc<>
的IoC
.
在引导程序的构造函数或方法中,为那些相互包装的静态委托Configure()
提供钩子,如下所示:Func<>
IoC
public class CalculatorBootstrapper : BootstrapperBase {
private Func<Type, string, object> _previousGet;
public override void Configure() {
_previousGet = IoC.GetInstance; // store reference to whatever was stored previously
IoC.GetInstance = this.GetInstance;
}
public override Object GetInstance(Type type, string key) {
var result = null;
if (_previousGet != null)
result = _previousGet(type, key);
if (result == null) {
// Try to use the local container here
}
return result;
}
}