gpt4 book ai didi

c# - Windsor Container 什么时候触发临时组件的 IKernel.ComponentDestroyed 事件?

转载 作者:太空宇宙 更新时间:2023-11-03 22:13:40 25 4
gpt4 key购买 nike

调用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 并添加一个退役步骤,从而强制跟踪该组件,这意味着您放入其中的代码将被调用发布时。

关于c# - Windsor Container 什么时候触发临时组件的 IKernel.ComponentDestroyed 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5803645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com