示例代码:
public interface IMyClass
{
string Name { get; set; }
}
public class MyClass : IMyClass
{
public string Name { get; set; }
}
public static class Program
{
public static void Main()
{
var container = new Container();
var class1 = new MyClass() {Name = "class 1"};
container.Configure(x => x.For<IMyClass>().Use(class1).Named("MyClass"));
var instance1 = container.TryGetInstance<IMyClass>("MyClass");
container.EjectAllInstancesOf<IMyClass>();
var class2 = new MyClass() { Name = "class 2" };
container.Configure(x => x.For<IMyClass>().Use(class2).Named("MyClass"));
var instance2 = container.TryGetInstance<IMyClass>("MyClass");
Console.ReadKey();
}
}
我希望instance2返回class2,但它返回class1。示例代码有什么问题吗?我需要做什么才能获得 class2?