在Autofac我怎么改变的是建立以后注册的实例已经叫什么名字?的是、叫什么名字、实例、我怎么

2023-09-03 09:30:34 作者:突然好想装逼

所以,可以说,我有这个code

So lets say i have this code

var builder = new ContainerBuilder();
builder.RegisterInstance(new MyType());
var container = builder.Build();

然后一段时间后,我想改变的的MyType 的实例,未来所有可解析呼吁容器

Then some time later I want to change the instance of MyType for all future resolves that are called on container.

推荐答案

目前要变更登记,创建一次新 ContainerBuilder ,注册了新的实例,并调用更新传递容器:

At the time you want to change the registration, create a new ContainerBuilder, register the new instance, and call Update passing in the container:

// at some later point...
builder = new ContainerBuilder();
builder.RegisterInstance(myType2);
builder.Update(container);