调用container.Release时没有触发事件,下面的测试总是失败:
public void ComponentDestroyedEvent()
{
var wasDestroyed = false;
var container = new WindsorContainer()
.Register(
Component.For(typeof (Cat))
.LifeStyle.Transient
.OnCreate((k, instance) => {
k.ComponentDestroyed += (model, component) => {
if (component == instance)
wasDestroyed = true;
};
}));
var cat = container.Resolve<Cat>();
container.Release(cat);
Assert.True(wasDestroyed);
}
ComponentDestroyed事件什么时候触发?
Mauricio 是对的 - 该组件不会触发该事件,因为该组件未被跟踪并且 Windsor 无论如何都不会与它有任何关系。
在跟踪组件的情况下,在运行所有停用步骤后,事件将作为管道的最后一步引发。
Windsor 3 推出时,有一个 OnDestroy
方法,该方法接受一个 lambda 并添加一个退役步骤,从而强制跟踪该组件,这意味着您放入其中的代码将被调用发布时。
我是一名优秀的程序员,十分优秀!