如何从Unity映射中删除(注销)已注册的实例? [英] How to remove(unregister) registered instance from Unity mapping?
问题描述
我遇到了一个我现在无法解决的问题. 我有以下内容:
I meet one problem that i can't solve now. I have the following:
UnityHelper.DefaultContainer.RegisterInstance(typeof(IMyInterface), "test", instance);
其中UnityHelper.DefaultContainer
是我的助手,用于获取具有已加载配置的统一容器.
where UnityHelper.DefaultContainer
is my helper for getting unity container with loaded configuration.
在这里,我将instance
注册为IMyInterface
的实例.
here I registered instance
as an instance of IMyInterface
.
因此,我想在任何地方(使用后一段时间)都删除此映射.完全删除.我该怎么办?
So anywhere( some time after using) I want to remove this mapping. Remove it at all. How I can do it?
我尝试过:
UnityHelper.DefaultContainer.Teardown(instance)
但未成功,并且以下代码始终返回instance
:
but is was unsuccessful and the following code returns instance
anyway:
UnityHelper.DefaultContainer.ResolveAll<IMyInterface>()
推荐答案
我遇到了同样的问题,只是从我的Container
中删除了ContainerControlledLifetimeManager
的注册:
I had the same problem and just removed the registrations of the ContainerControlledLifetimeManager
from my Container
:
foreach (var registration in container.Registrations
.Where(p => p.RegisteredType == typeof(object)
&& p.Name == name
&& p.LifetimeManager.Type == typeof(ContainerControlledLifetimeManager)))
{
registration.LifetimeManager.RemoveValue();
}
这篇关于如何从Unity映射中删除(注销)已注册的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!