2011-01-20 176 views
0

我得到了以下測試:相同的實例

[TestMethod] 
    public void TestTwoInterfacesOneNamed() 
    { 
     _container.RegisterType<MyTestClass, MyTestClass>(new ContainerControlledLifetimeManager()); 
     _container.RegisterType(typeof(ITestClass), typeof(MyTestClass)); 
     _container.RegisterType(typeof(IStartable), typeof(MyTestClass), "nameSoThatAllStartablesCanBeFetched"); 
     var resolveAll = _container.ResolveAll<IStartable>(); 
     Assert.AreSame(_container.Resolve(typeof(ITestClass)), resolveAll.First()); 
    } 

如何獲得兩個解決了同一個實例?只要沒有命名,註冊具體的技巧首先適用於所有接口。我不想註冊一個實例。

回答

1

它的完成這樣的:

 Func<IUnityContainer, object> resolver = (unity) => unity.Resolve(typeof(MyTestClass)); 
     _container.RegisterType(typeof(IStartable), 
      typeof(MyTestClass), 
      "nameSoThatAllStartablesCanBeFetched", 
      new InjectionFactory(resolver));